Write a program to manipulate a C-string entered by the user. Assume the maximum
ID: 3821628 • Letter: W
Question
Write a program to manipulate a C-string entered by the user. Assume the maximum size of the C-string including the null character is 80. The user should able to enter whitespace characters as well. Your program must prompt the user to enter some text. After the user is finished with entering the text, parse your array and do the following things i) Compute the length of the text ii) Remove every special character (i.e. all characters except alphabets A-Z, a-z and digits 0-9) iii) If alphabet, change lower case to upper case and upper case to lower case iv) If digit, add one it i.e. 5 needs to become 6. Change 9 to 0. v) Compute the length of the new C-string. Now display this updated text on the screen.Explanation / Answer
// C code
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
int main() {
char inputString[80], newString[80];
int count = 0, i, originalStringLength, newlength;
printf("Enter string: ");
scanf("%[^ ]s", inputString);
originalStringLength = strlen(inputString);
for(i=0;i<originalStringLength;i++)
{
if(inputString[i]>='a'&&inputString[i]<='z')
newString[count++] = inputString[i]-('a'-'A');
else if(inputString[i]>='A'&&inputString[i]<='Z')
newString[count++] = inputString[i]+('a'-'A');
else if(inputString[i]>='0'&&inputString[i]<='9')
newString[count++] = '0' + (inputString[i]-'0'+1)%10;
}
newString[count] = '';
newlength = strlen(newString);
printf(" Original length: %d ", originalStringLength);
printf("Original string: %s ", inputString);
printf("New length: %d ", newlength);
printf("New string: %s ", newString);
return 1;
}
/*
output:
Enter string:
my name is @#$new5Name. I &l(ive i3n vir23ginia
Original length: 47
Original string: my name is @#$new5Name. I &l(ive i3n vir23ginia
New length: 34
New string: MYNAMEISNEW6nAMEiLIVEI4NVIR34GINIA
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.