Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program to manipulate a C-string entered by the user. Assume the maximum

ID: 3817490 • 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. HINT: Remember user needs to able to enter whitespace, what C-string function needs to be invoked? Parse through the C-string one character at a time in a loop, and check what character you have. If it’s an alphabet or a digit, store it a second C-string by making the required changes, if not just skip to the next character. Make sure your new C-string also has a NULL character.

Explanation / Answer

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>

void uppercase_conversion(char []);
void lowercase_conversion(char []);
void add_to_number(char []);
int main()
{
char arr[79];
int i, j,l,l1;
printf("Enter a string: ");
gets(arr);
l= sizeof(arr);
// to print the length of the string
printf("Length of string entered : %d",l/sizeof(arr[0]));
  
//remove all special characters except alphabets A-Z, a-z and digits 0-9

for(i = 0; arr[i] != ''; ++i)
{
while (!( (arr[i] >= 'a' && arr[i] <= 'z') || (arr[i] >= 'A' && arr[i] <= 'Z') ||(arr[i] >= 0 && arr[i] <= 9) || arr[i] == '') )
{
for(j = i; arr[j] != ''; ++j)
{
arr[j] = arr[j+1];
}
arr[j] = '';
}
}
  
// Change the case of any alphabet letter
uppercase_conversion(arr);
lowercase_conversion(arr);
// add +1 if its a number
add_to_number(arr);
  
printf("Entered string after case conversion is "%s" ", arr);

l1= sizeof(arr);
// to print the length of the new string
printf("Length of new string transformed : %d",l1/sizeof(arr[0]));

  
}

// function to convert lowercase into upper case which is being invoked from the main
void uppercase_conversion(char s[]) {
int c = 0;

while (s[c] != '') {
if (s[c] >= 'a' && s[c] <= 'z') {
s[c] = s[c] - 32;
}
c++;
}
}

// function to convert upper case in to lower case which is being invoked from the main
void upper_string(char s[]) {
int c = 0;

while (s[c] != '') {
if (s[c] >= 'A' && s[c] <= 'Z') {
s[c] = s[c] + 32;
}
c++;
}
}

// function to add +1 to a numeric value and convert 9 to 0
void add_to_number(char s[]) {
int c = 0;

while (s[c] != '')
// add 1 to a numeric value between 0 to 8
{
if (s[c] >= 0 && s[c] <= 8) {
s[c] = s[c] + 1;
}
// convert to 0 if the letter is 9
if (s[c] = 9)
{
s[c]=0
}
c++;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote