In: Computer Science
In a c programming
Write a program that converts upper case letters to lower case letters or vice versa:
Enter a sentence: What a GREAT movie is!
Converted sentence: wHAT_A_great_MOVIE_IS_
Convert all non-alphabetical letters to ‘_’
Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate
the question. Thank You So Much.
code.c
#include <stdio.h>
void convertCase(char * line){
int i=0;
char c;
while (line[i] != '\0')
{
//get the char
c = line[i];
//convert to ascii
int ascii = c;
//if 97 to 122 is lowercase
range
//convert to ascii back to
char
char converted ;
if(ascii>=97 &&
ascii<=122 ){
//difference
between uppercase and lower case is 32. if we deduct 32 from lower
case it will convert to uppercase
ascii-=32;
//range 65 to 90
is uppercase range
converted =
ascii;
}else if(ascii>=65 &&
ascii<=90){
//if we will add
32 to upper case it will convert to lower case
ascii+=32;
converted =
ascii;
}else{
converted =
'_';
}
line[i] = converted;
i=i+1;
}
}
int main()
{
char line[100];
printf("Enter a sentence: ");
gets(line);
convertCase(line);
printf("Converted sentence: %s",line);
return 0;
}