How do I end the loop in the \'main\' function below. Need to end program if ans
ID: 3878586 • Letter: H
Question
How do I end the loop in the 'main' function below. Need to end program if answer is 'N' and get back to the first step if its 'Y'.
void reversedWord (char input [], int size)
{
char a[50];
int i=0, j, temp;
printf("Please enter a word: ")
scanf(" %s", a);
i=0;
j= strlen(a)-1;
while (i<j)
{
temp= a[i];
a[i]= a[j];
a[j]= temp;
i++;
j--;
}
int main()
{
printf("Your word spelled backwards is: %s", a);
getchar();
char answer;
printf(" Would you like to continue (Y/N)?: ",answer);
scanf(" %s", &answer);
if(answer=="N")
{
printf(" Thank you! Goodbye!");
}
else(answer=="Y");
return 0;
}
Explanation / Answer
Given below is the fixed code for the question.
Please do rate the answer if it was helpful. Thank you
#include <stdio.h>
#include <string.h>
void reversedWord (char a[])
{
int i, j, temp;
i = 0;
j = strlen(a)-1;
while (i<j)
{
temp= a[i];
a[i]= a[j];
a[j]= temp;
i++;
j--;
}
}
int main()
{
char a[50];
char answer;
do
{
printf("Please enter a word: ");
scanf("%s", a);
getchar(); //get rid of newline
reversedWord(a);
printf("Your word spelled backwards is: %s ", a);
printf(" Would you like to continue (Y/N)?: ");
answer = getchar();
}while(answer == 'Y' || answer == 'y');
printf(" Thank you! Goodbye!");
return 0;
}
output
=======
Please enter a word: hello
Your word spelled backwards is: olleh
Would you like to continue (Y/N)?: y
Please enter a word: morning
Your word spelled backwards is: gninrom
Would you like to continue (Y/N)?: n
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.