Write a C Program with a main() function that declares the arrays and loops unti
ID: 3772128 • Letter: W
Question
Write a C Program with a main() function that declares the arrays and loops until the user answers Y to the question “do you want to quit?”
main
declare two (2) char arrays that can hold 80 characters each. ca1 and ca2
do
send ca1 to getString with prompt “enter string 1”
send ca2 to getString with prompt “enter string 2”
send ca1 and ca2 to doStrings
printf prompt “Do you want to Quit?”
read answer into one of the arrays
while (answer != “Y” )
end main
void getString( two parameters )
1. character array to fill
2. character array with prompt
print prompt
read what user types into 1. array to fill
end getString
void doStrings ( two parameters )
1. first string
2. second string
loop do
print menu
1. show strings Use “first string >%s%s< ” formats
2. compare strings call strcmp() and use if else to print “%s is less than %s ” or equal to or greater than
3. show lengths
4. trim first string
5. trim second string
6. reverse first
7. reverse second
10. get new strings
read int
perform proper action (or print error message) using a switch statement
while (int != 10 )
end doStrings
Explanation / Answer
#include <stdio.h>
#include <string.h>
void getString(char ca1[],char prompt[])
{
printf("%s ", prompt);
fgets(ca1,80,stdin);
if(strlen(ca1)>0&&ca1[strlen(ca1)-1]==' ')
ca1[strlen(ca1)-1] = '';
}
void reverse(char string1[], char prompt[])
{
int i;
int last = strlen(string1)-1;
int length = strlen(string1);
char c;
for(i=0;i<length/2;i++)
{
c = string1[last];
string1[last] = string1[i];
string1[i] = c;
last--;
}
printf("%s",prompt);
printf("%s ",string1);
}
void trim(char* string1, char prompt[])
{
int actual = 0;
int modified = 0;
int i;
for(i=0;string1[i]!='';i++)
{
if(string1[actual]!=' ')
{
string1[modified] = string1[actual];
modified++;
}
actual++;
}
string1[modified] = '';
printf("%s",prompt);
printf("%s ",string1);
}
void doStrings(char ca1[], char ca2[])
{
int choice,comparision,length;
do
{
printf(" 1.show strings ");
printf("2.compare strings ");
printf("3.show lengths ");
printf("4.trim first string ");
printf("5.trim second string ");
printf("6.reverse first ");
printf("7.reverse second ");
printf("10.get new strings ");
printf("Enter your choice::");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("first string>%s second string>%s ", ca1,ca2);
break;
case 2:
comparision = strcmp(ca1,ca2);
if(comparision==0)
printf("%s is equal to %s ", ca1,ca2);
else if(comparision>0)
printf("%s is greater than %s ", ca1,ca2);
else
printf("%s is less than %s ", ca1,ca2);
break;
case 3:
length = strlen(ca1);
printf("Length of string1 is %d ", length);
length = strlen(ca2);
printf("Length of string2 is %d ", length);
break;
case 4:
trim(ca1,"trimmed string1::");
break;
case 5:
trim(ca2,"trimmed string1::");
break;
case 6:
reverse(ca1,"reversed string1::");
break;
case 7:
reverse(ca2,"reversed string1::");
break;
case 10:
break;
default:
printf("Enter a valid choice ");
break;
}
}while(choice!=10);
}
int main()
{
char ca1[80],ca2[80];
do
{
getString(ca1,"enter string 1");
getString(ca2,"enter string 2");
doStrings(ca1,ca2);
printf("Do you want to Quit? Press Y to quit ");
scanf("%s",ca1);
}while(!((ca1[0]=='Y'||ca1[0]=='y')&&ca1[1]==''));
return 1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.