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

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

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