In: Computer Science
You cna hand write this if you want, Please code this in C Thank you PROBLEM DESCRIPTION: Write a program to implement the following requirement: The program will read from standard input two things - a string str1 on the first line of stdin (this string may be an empty string) - a string str2 on the second line of stdin (this string may be an empty string) Note that stdin does not end with '\n'. The program will output a string that is the concatenation of string str1 and string str2 such that any lower-case alphabet character (a-z) will be converted to upper-case and any upper-case alphabet character (A-Z) will be converted into lower-case. Note that stdout does not end with '\n'. You cannot use any existing function from any library to do this concatenation. The maximum length of each input string is 100 characters SAMPLE INTPUT this is string one This is string two SAMPLE OUTPUT THIS IS STRING ONEtHIS IS STRING TWO
Solution code is given below with explanation in comments:
#include <stdio.h>
int main()
{
// Variables to hold strings.
char str1[100];
char str2[100];
char str[200];
// Taking input from stdin.
fgets(str1, 100, stdin);
fgets(str2, 100, stdin);
int i=0,j;
// To copy the first string into the str.
// We are making use of '\0' as every string ends by this
character.
// Loop will continue until we encounter the null character
'\0'.
// Also we will ignore the next line character '\n'.
for(j=0; str1[j]!='\0'; j++) {
if(str1[j]!='\n') {
// For converting to upper case or lower case we are making
use
// of ASCII values of the characters as characters are also
treated
// as numbers by the language.
// ASCII code for A to Z is 65 to 90.
// ASCII code for a to z is 97 to 122.
// There is difference of 32 in the upper and lower case values
of
// each character so just using it for conversion.
if(str1[j] >= 'a' && str1[j] <= 'z')
str[i] = str1[j]-32;
else if(str1[j] >= 'A' && str1[j] <= 'Z')
str[i] = str1[j]+32;
else
str[i] = str1[j];
i++;
}
}
// Second loop keeping the i as it is from the previous loop.
// Everything is similar to the above loop.
for(j=0; str2[j]!='\0'; j++) {
if(str2[j]!='\n') {
if(str2[j] >= 'a' && str2[j] <= 'z')
str[i] = str2[j]-32;
else if(str2[j] >= 'A' && str2[j] <= 'Z')
str[i] = str2[j]+32;
else
str[i] = str2[j];
i++;
}
}
// Ending the string by the null character.
str[i] = '\0';
// Printing the resulted string.
printf("%s", str);
return 0;
}
Screenshots: