Showing posts with label Letter. Show all posts
Showing posts with label Letter. Show all posts

Lower-Upper Case/Positive-Negative Integer

How to let C Program to recognize both lower and upper case input. Also Check if input is a positive integer, negative integer in C Programming Language ?


This C Program is to recognize both lower and upper case input. Also Check if input is a positive integer, negative integer in C Programming Language.

Output:

  1. Your input is positive even digit
  2. Your input is negative odd digit
  3. Your input is upper case letter
  4. Your input is lower case letter

Solution :

  1. #include<stdio.h>
  2. #include<string.h>
  3. void main()
  4. {
  5.     char array[20];
  6.     int i;
  7.   for(i=0;i<5;i++){
  8.     gets(array);
  9.   if(strlen(array)==1){
  10.   if(array[i]>=48 && array[i]<=57){
  11.     if(array[i]%2==0)
  12.      printf("Your input is positive even digit\n");
  13.     else
  14.      printf("Your input is negative odd digit\n");
  15.   }
  16.   if(array[i]>=65 && array[i]<=90)
  17.     printf("Your input is upper case letter\n");
  18.   else if(array[i]>=97 && array[i]<=122)
  19.     printf("Your input is lowercase letter\n");
  20.   }
  21.   }
  22. }

C Program to count number of sentences words and letters in a paragraph

How to write a C Program to count number of sentences words and letters in a paragraph in C Programming Language ?

This C Program to count number of sentences words and letters in a paragraph.

Solution:

  1. #include<stdio.h>
  2. main()
  3. {
  4.         char ch[150];
  5.         int i,c=0,c1=0,c2=0;
  6.         printf("Enter a Paragraph \n");
  7.         gets(ch);
  8.         for(i=0;ch[i]!='\0';i++)
  9.         {
  10.                 if(ch[i]=='.'||ch[i]=='!'||ch[i]=='?')
  11.                 {       c++;
  12.                         c1++;
  13.                 }
  14.                 else if(ch[i]==' '||ch[i]==',')
  15.                         c1++;
  16.                 else if(ch[i]>65&&ch[i]<123)
  17.                         c2++;
  18.         }
  19.         printf("\nNumber of letters = %d \nNumber of words=%d ",c2,c1);
  20.         printf("\nNumber of Sentences = %d\n",c);
  21. }

C Program Anagrams

How to write a C Program Anagrams in C Programming Language ?

Considering a string will only contain lower-case alphabets. It can also be done for uppercase letters , digits , special characters etc.

Solution:

  1. #include<stdio.h>
  2. #include<stdbool.h>
  3. #include<string.h>
  4. bool isAnagram(char *str1,char *str2)
  5. {
  6.     int alphabetCount[26] = {0};
  7.     int i = 0;
  8.    
  9.     if(strlen(str1) != strlen(str2))
  10.         return false;
  11.    
  12.     for(i=0;str1[i]!='\0';++i)
  13.     {
  14.         ++alphabetCount[str1[i]-'a'];
  15.         --alphabetCount[str2[i]-'a'];
  16.     }
  17.    
  18.     for(i=0;i<26;++i)
  19.     {
  20.         if(alphabetCount[i])
  21.             return false;
  22.     }
  23.    
  24.     return true;
  25. }
  26.  
  27. int main(void)
  28. {
  29.    char string1[1001],string2[1001];
  30.    
  31.    /*
  32.      considering a string will only contain lower-case alphabets
  33.     It can also be done for uppercase letters , digits , special characters etc.
  34.     */
  35.    
  36.    gets(string1);
  37.    gets(string2);
  38.    
  39.    if(isAnagram(string1,string2))
  40.        printf("yes");
  41.    else
  42.        printf("no");
  43.    
  44.     return 0;
  45. }

File Handling (console to file) in C Program

How to write a C Program Writing a character into a file,reading the character from the console and writing the next letter into the file in C Programming Language ?



Solution:
/* Writing a character into a file,reading the character from the console and writing the next letter into the file*/

#include<stdio.h>
#include<stdlib.h>

int main()
{

FILE *fp; //creates a file pointer
char ch;
int con;


fp=fopen("filehandling.txt","w+"); // creates a text file with label "filehandling" in write mode

if(fp==NULL)  //checks for the filename validity
{
printf("\nUnable to open the file\n");
exit(0);
}
printf("\nFile opened successfully\n");

printf("\nEnter the character as input\n");
scanf("%c",&ch);


putc(ch,fp);

fclose(fp);

fp=fopen("filehandling.txt","rwa+");

ch=getc(fp);


con=ch;
con++;
if(con>=97 && con<122)
{

fprintf(fp,"\nthe next char is %c\n",con);
}
else
{
fprintf(fp,"\nthe next char is a\n");
fclose(fp);

// open filehandling.txt file and check the input and output given...
}
}