Friday, March 30, 2018

C Program for String manipulation.

/*C program to count upper case, lower case and special characters in a string.*/
#include<stdio.h>
int main()
{
char text[100];
int i;
int countL,countU,countS;
printf("Enter any string: ");
gets(text);
/* here, we are printing string using printf
//without using loop */
printf("Entered string is: %s\n",text);
/* count lower case, upper case and special characters
//assign 0 to counter variables */
countL=countU=countS=0;
for(i=0;text[i]!='\0';i++)
{
/* check for alphabet */
if((text[i]>='A' && text[i]<='Z') || (text[i]>='a' && text[i]<='z'))
{
if((text[i]>='A' && text[i]<='Z'))
{
/* it is upper case alphabet */
countU++;
}
else
{
/* it is lower case character */
countL++;
}
}
else
{
/* character is not an alphabet */
countS++; /*it is special character */
}
}
/*convert into upper case */
for(i=0;text[i]!='\0';i++)
{
/*check character is alphabet or not */
if((text[i]>='A' && text[i]<='Z')||(text[i]>='a' && text[i]<='z'))
{
/* check for upper case character */
if(text[i]>='A' && text[i]<='Z')
text[i]=text[i]+0x20;
else
text[i]=text[i]-0x20;
}
}
printf("String is: ");
for(i=0;text[i]!='\0';i++)
{
printf("%c",text[i]);
}
printf("\n");
printf("VOWEL Characters are: ");
for(i=0;text[i]!='\0';i++)
{
if(text[i]=='A' || text[i]=='a' || text[i]=='E' || text[i]=='e' || text[i]=='I' || text[i]=='i' || text[i]=='O' || text[i]=='o' || text[i]=='U' || text[i]=='u')
printf("%c",text[i]);
}
printf("\n");
printf("CONSONANT Characters are: ");
for(i=0;text[i]!='\0';i++)
{
if(!(text[i]=='A' || text[i]=='a' || text[i]=='E' || text[i]=='e' || text[i]=='I' || text[i]=='i' || text[i]=='O' || text[i]=='o' || text[i]=='U' || text[i]=='u'))
printf("%c",text[i]);
}
printf("String after toggle case: %s\n",text);
printf("Upper case characters: %d\n",countU);
printf("Lower case characters: %d\n",countL);
printf("Special characters: %d\n",countS);
return 0;
}

Output:-

String manipulation

No comments:

Post a Comment

Life Articles And News:

Life Articles And News: