Showing posts with label Char. Show all posts
Showing posts with label Char. Show all posts

Max count of chars, Max count of nums and Max count of signs

How to write a C Program to Maximum count of chars, Maximum count of numbers and Maximum count of signs in C Programming Language ?

This C Program to Max count of chars, Max count of nums and Max count of signs.

Solution:


  1. #include <stdio.h>
  2. #include <conio.h>
  3. void main() {
  4.         int chars=0, nums=0, mchars=0, mnums=0, signs=0, msigns = 0, i;
  5.         char str[500];
  6.         puts("Input string:");
  7.         gets(str);
  8.         for (= 0; str[i]; i++) {
  9.                 if (str[i] >= '0' && str[i] <= '9') {
  10.                         nums++;
  11.                         if (chars > mchars) mchars = chars;
  12.                         if (signs > msigns) msigns = signs;
  13.                         chars = signs = 0;
  14.                 }
  15.                 else {
  16.                         if (nums > mnums) mnums = nums;
  17.                         nums = 0;
  18.                         if ((str[i] >= 'a' && str[i] <= 'z') ||
  19.                                 (str[i] >= 'A' && str[i] <= 'Z')) {
  20.                                 chars++;
  21.                                 if (signs > msigns) msigns = signs;
  22.                                 signs = 0;
  23.                         }
  24.                         else if (str[i] == '*' || str[i] == '+' || str[i] == '-'){
  25.                                 signs++;
  26.                                 if (chars > mchars) mchars = chars;
  27.                                 chars = 0;
  28.                         }
  29.                         else {
  30.                                 if (chars > mchars) mchars = chars;
  31.                                 if (signs > msigns) msigns = signs;
  32.                         }
  33.                 }
  34.         }
  35.         if (nums > mnums) mnums = nums;
  36.         if (chars > mchars) mchars = chars;
  37.         if (signs > msigns) msigns = signs;
  38.         printf("Max count of chars: %d\nMax count of nums: %d\nMax count of signs: %d\n", mchars, mnums, msigns);
  39.         printf("Is chars count bigger than signs count: ");     printf(mchars > msigns ? "true" : "false");
  40.         _getch();
  41. }

C Program Char Match

How to write a C Program to Char Match in C Programming Language ?


Solution:

  1. #include<stdio.h>
  2. #include<string.h>
  3. int main()
  4. {
  5.     char a[50], b[50];
  6.  
  7.     scanf("%s",a);
  8.     scanf("%s",b);
  9.  
  10.     int firstWord[26];
  11.  
  12.     int i, c=0;
  13.  
  14.     for (i=0; i<26; i++) {
  15.         firstWord[i]=0;
  16.     }
  17.  
  18.     for (i=0; i<strlen(a); i++ ) {
  19.         firstWord[a[i] - 'a'] = 1;
  20.     }
  21.     for (i=0; i<strlen(b); i++) {
  22.         if (firstWord[b[i]-'a'] == 1) c++;
  23.     }
  24.     printf("%d",c);
  25. }

C Program that prompts the user to input a string, (whitespaces allowed)

How to Write a program that prompts the user to input a string, (whitespaces allowed)
Add code to:
Step 1: allocate memory for the string
Step 2: Scanf the string from the user
Step 3: Write a function to calculate the length of the string.
            int length(char name[]);Â


Solution:

/*Write a program that prompts the user to input a string, (whitespaces allowed)
Add code to:

step1: allocate memory for the string
step2: Scanf the string from the user
step3: Write a function to calculate the length of the string.
int length(char name[]);Â


*/
#include<stdio.h>
#define SIZE 41

int main()
{ 
 //Define student name
 char name[SIZE];
 printf("Please enter your name:");
 scanf("%[^\n]",name);
 
 printf("The length of the name is %d",length(name));
 getch();
}

int length(char name[])
{
 int i,result=0;
 for(i=0;name[i]!='\0';i++)
  result++;
  
 return result;

}

C Program Read a char & print next char ( file to file )

How to write a c program to read a char & print next char ( file to file ) in C Programming Language ?


Solution:
/* Read a char & print next char ( file to file ) : A character should be written in the file named "readfile.txt" beforehand and we will take this as input to be manipulated and finally written into the file named "writefile.txt" */