Question: I'm having trouble getting my program to have the output i want Output Sample: ./a.out Enter a string (up to 25 characters) to be transformed:

I'm having trouble getting my program to have the output i want

Output Sample:

./a.out

Enter a string (up to 25 characters) to be transformed: Go Tigers

Enter your command (quit, replace all): REPLACE ALL

Enter the character to replace: e

Enter the new character: 3

Your new sentence is: Go Tig3rs

Enter your command (quit, replace all): replace some aaaaaaaaaaaaa12341234

Sorry, that command is invalid. Please type one of the options.

Enter your command (quit, replace all): replace all

Enter the character to replace: i

Enter the new character: 1

Your new sentence is: Go T1g3rs

Enter your command (quit, replace all): rePLace ALL

Enter the character to replace: q

Error, q is not in the string.

Enter your command (quit, replace all): QUIT

Here's my code:

#include

#include

#include

void stringReplace(){

char sentence[26];

char command[15];

char replace;

char newSentence;

int flag=0;

printf("Enter a string (up to 25 characters) to be transformed: ");

scanf("%[^ ]%*c", sentence);

while(1){

printf("Enter your command (quit, replace all): ");

if(flag==1){

getchar();

}

if(flag==0){

flag=1;

}

scanf("%[^ ]%*c", command);

int lettercommand=0;

while(command[lettercommand]!=0){

if(command[lettercommand]>='A' && command[lettercommand]<='Z'){

command[lettercommand]=command[lettercommand]+32;

}

lettercommand++;

}

printf("command::%s ", command);

if(strcmp(command,"quit")==0){

break;

}

else if(strcmp(command,"replace all")==0){

printf("Enter the character to be replaced::");

scanf("%c", &replace);

int flag_replacement=1;

int index=0;

while(sentence[index]!='\0'){

if(sentence[index]==replace){

flag_replacement=0;

break;

}

index=index+1;

}

printf("after replacing with new character::%s ",sentence);

flag_replacement=0;

}

else{

printf("Please enter either quit or replace all ");

}

}

}

int main(){

stringReplace();

return(0);

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!