In: Computer Science
Write a program that asks the user to enter a sentence and then switch all lower case letters to capitals and all upper case letters to lower. HINT: use the ASC function to tell your program which letters are upper or lower case so you can switch them. Your program should allow for multiple test cases.
Below is the solution:
code:
#include <stdio.h>
int main() {
char s[100]; //declare the char array
printf("\nEnter a string : "); //ask to enter the
string
gets(s); //get from keyboard
ASC(s); //call the function to uppercase to lower and
lower case to upper
return 0;
}
void ASC(char s[100]){
int i; //declare the value
printf("\nString: "); //print
for (i = 0; s[i]!='\0'; i++) { //loop each of
the char until null
if(s[i] >= 'a' && s[i]
<= 'z') { //check for the lower case
s[i] = s[i] - 32;
//subtract the value to print the uppercase
printf("%c",
s[i]); //print the character
}
else if(s[i] >= 'A' &&
s[i] <= 'Z') {
s[i] = s[i] + 32;
//add the value to print the lowercase
printf("%c",
s[i]); //print the character
}
else if(s[i] == ' ') {
printf("%c",s[i]); //prints the space
}
else{ //prints the other
characters
printf("%c",s[i]); //prints
}
}
}
sample output:
Enter a string : This is The tESt sTriNg. String: tHIS IS tHE TesT StRInG.