C-Programming
How to write a program in c to convert upper case to lower case and vice versa of a string ?
convert upper case to lower case and vice versa of a string.
Steps of programming :-
- First we Initialize a variable as int and another as string (e.g char s[100] int i)
- then we get input of a string in s.
- then we start a loop from i =0 to i=100 here i is position of char in string.this loop will give one by one all character of string and we will change that.
- as we know that ASCII value of, A-Z is b/w 65-90, and a-z is b/w 97-122. he we can see 65+32=97 means A+32=a,B+32=b an so on.
- so we use s[i]=s[i]-32 for convert for convert to convert small latter to capital latter and s[i]=s[i]+32 for convert for convert to convert capital latter to small latter
- then our program will complete.
CODE:-
#include<stdio.h>
#include<string.h>
void main()
{int i;
char s[100];
clrscr();
printf("\nType Here : ");
gets(s);
for(i=0;i!=100;i++)
{
if(s[i]>=97 && s[i]<=122)
s[i]=s[i]-32;
else if(s[i]>=65 && s[i]<=90)
s[i]=s[i]+32;
}
printf("\nString after changing case =%s",s);
getch();
}
Thank-you
No comments:
Post a Comment