In: Computer Science
Program 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
Note: here each line of code is explained in the comment section, but if you any error or issue or dont understand the code feel free to ask me or post the issue.
(Remove that two scanf() comment code after fgets(), used only for checking the code).
myString.c
#include<stdio.h>
#define MAX_LIMIT 100
int main(){
char str1[MAX_LIMIT], str2[MAX_LIMIT], res[2*MAX_LIMIT];
int i = 0, k = 0;
fgets(str1, MAX_LIMIT, stdin);
fgets(str2, MAX_LIMIT, stdin);
// scanf("%[^\n]%*c",str1);
// scanf("%[^\n]%*c",str2);
// for str1
for(i=0; str1[i]!='\0'; i++){
// store str1[i] to char ch
char ch = str1[i];
// if ch is between 'A' to 'Z'
if(ch >= 'A' && ch <= 'Z')
str1[i] += 32;
// if ch is between 'a' to 'z'
else if(ch >= 'a' && ch <= 'z')
str1[i] -= 32;
// check for next line ch if found continue loop
else if(ch == '\n')
continue;
// store to res array
res[k++] = str1[i];
}
// for str2
for(i=0; str2[i]!='\0'; i++){
char ch = str2[i];
// if ch is between 'A' to 'Z'
if(ch >= 'A' && ch <= 'Z')
str2[i] += 32;
// if ch is between 'a' to 'z'
else if(ch >= 'a' && ch <= 'z')
str2[i] -= 32;
// check for next line ch if found continue loop
else if(ch == '\n')
continue;
// store to res array
res[k++] = str2[i];
}
// store last char with null to print string before NULL value
res[k] = '\0';
// print res string
printf("%s",res);
return 0;
}