Saturday, May 21, 2011

C Program to perform operation on Strings (Array of Character)

Read a line of text into a character array and perform the following
operation:
a. Count the number of Upper case , Lower case, digits and
special characters.
b. Convert Upper case to Lower case and vice versa without
using string library functions.
c. Count the number of words, lines and characters in a given
text.


main()
{
char a[40];
int i=0,lc=0,uc=0,w=0,le=0,d=0,s=0;
printf("Enter string");
gets(a);

while(a[i]!='\0')
{
if(a[i]>='a' && a[i]<='z')
{
lc++;
a[i]=a[i]-32;
le++;
}
else if (a[i]>='A' && a[i]<='Z')
{
uc++;
a[i]=a[i]+32;
le++;
}
else if (a[i]==' ')
w++;
else if(a[i]=='.')
s++;
else if (a[i]>='0' && a[i]<='9' )
d++;

i++;
}
printf("%s\n",a);
printf("Upper case %d\n",uc);
printf("Lower case %d\n",lc);
printf("Digits %d\n",d);
printf("Sentences/lines %d\n",s);
printf("Number of words %d\n",++w);
getch();
}

No comments:

Post a Comment

Google+ Followers