Home > database >  Mismatch output in C using while and for loop
Mismatch output in C using while and for loop

Time:10-09

I'm attempting to print out my reverse string with an additional dialogue for userInput as well as a continuous userInput until the the 3 factors are true. While my program works I cant seem to figure the order in which to print for it to match what the auto-grader wants.

Currently the program gets the userInput and prints it out but only after asking for the continuous input. I need to figure how to print it right below each statement and then the additional input after. enter image description here

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

int main () {
   
   char userInput[50];
   int length;
   
   printf("Please enter a line of text (maximum 50 characters). Type done, Done, or d to exit program.\n");
   
   fgets(userInput, 50, stdin);
   userInput[strlen(userInput) - 1] = '\0';
   
   //compare userInput, while there userIput doesnt equal done, Done, or d loop for input
   while (strcmp(userInput, "done") != 0 && strcmp(userInput, "Done") != 0 && strcmp(userInput, "d") != 0) {
      printf("Please enter a line of text (maximum 50 characters). Type done, Done, or d to exit program.\n");
      length = strlen(userInput);
    //print userInput  
   for (int i = length - 1; i >= 0; --i) {
      printf("%c", userInput[i]);
   }
   printf("\n");
   fgets(userInput, 50, stdin);
   userInput[strlen(userInput) - 1] = '\0';
   }
   return 0;
}

CodePudding user response:

You're very close .. simply need to move the prompt further down in the loop. Also note the subsequent "enter" message is different from the initial one, the autograder may be checking for that.

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

int main () {
   char userInput[50];
   int length;
   
   printf("Please enter a line of text (maximum 50 characters). Type done, Done, or d to exit program.\n");
   
   fgets(userInput, 50, stdin);
   userInput[strlen(userInput) - 1] = '\0';
   
   //compare userInput, while there userIput doesnt equal done, Done, or d loop for input
   while (strcmp(userInput, "done") != 0 && strcmp(userInput, "Done") != 0 && strcmp(userInput, "d") != 0) {
        // don't prompt for the next string here, print the reverse first
        // printf("Please enter another line of text (maximum 50 characters). Type done, Done, or d to exit program.\n");
        length = strlen(userInput);
        //print userInput  
        for (int i = length - 1; i >= 0; --i) {
            printf("%c", userInput[i]);
        }
        printf("\n");
        // move the next prompt to after the string reversal output
        printf("Please enter another line of text (maximum 50 characters). Type done, Done, or d to exit program.\n");
        fgets(userInput, 50, stdin);
        userInput[strlen(userInput) - 1] = '\0';
    }
    return 0;
}

Demonstration

CodePudding user response:

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

int main () {
   
   char userInput[50];
   int length;
   
   printf("Please enter a line of text (maximum 50 characters). Type done, Done, or d to exit program. \n");
   
   fgets(userInput, 50, stdin);
   userInput[strlen(userInput) - 1] = '\0';
   
   //compare userInput, while there userIput doesnt equal done, Done, or d loop for input
   while (strcmp(userInput, "done") != 0 && strcmp(userInput, "Done") != 0 && strcmp(userInput, "d") != 0) {
      length = strlen(userInput);
    //print userInput  
   for (int i = length - 1; i >= 0; --i) {
      printf("%c", userInput[i]);
   }
   printf("\n");
   printf("Please enter another line of text (maximum 50 characters). Type done, Done, or d to exit program.\n");
   fgets(userInput, 50, stdin);
   userInput[strlen(userInput) - 1] = '\0';
   }
   return 0;
}

there issue was that I had posted my new prompt in the while loop. It needed to be printed after both the while and nested IF loop. the new line bumps the prompt down giving the user their output and then asking for the prompt in the correct manner.

CodePudding user response:

Use function for this kind of tasks.

char *reverseString(char *str)
{
    char *start = str, *end = str;
    if(str && *str)
    {
        while(*(end   1)) end  ;
        while(end > start)
        {
            char tmp = *start;
            *start   = *end;
            *end-- = tmp;
        }
}
    return str;
}

char *removenewline(char *str)
{
    char *wrk = str;
    if(str && *str)
    {
        while(*str) str  ;
        if(*(str - 1) == '\n') *(str - 1) = 0;
    }
    return wrk;
}

int main(void)
{
    char str[256] = "";
    fgets(str, sizeof(str), stdin);
    removenewline(str);
    printf("Reversed string: `%s`\n", reverseString(str));
}    
  • Related