C Programming the printing of string backwords using pointers

My program will get the character string from the user and print the sentence back and it will keep asking the user for another line until the string equals quit. The problem I'm running into is when the user goes to the end of the loop when I want him to just type "thank you". I have no idea why the if and else statements don't work when the user navigates to the output.

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

int main()
{
    char chr;
    char *cPtr;
    char someString[50];
    int stringSize;
    int indx;

    printf("Enter a string of characters: ");
    cPtr = someString;

    while ((chr = getchar()) != '\n')
    {
        *cPtr = chr;
        cPtr++;
    }
    *cPtr = '\0';

    stringSize = strlen(someString);

    if (someString == "quit" )
    {
        printf("Thank you.");
    }
    else
    {
        while (someString != "quit")
        {
            for (indx = stringSize; indx >= 0; indx--)
            {
                printf("%c",*cPtr--);
            }

            printf("\nEnter a string of characters: ");
            cPtr = someString;

            while ((chr=getchar())!= '\n')
            {
                *cPtr = chr;
                cPtr++;
            }
            *cPtr = '\0';

            stringSize = strlen(someString);
        }
    }
}

      

+3


source to share


1 answer


if (someString == "quit" )

does not compare the contents of two strings. He compares their addresses, which are always different. Use a function instead strcmp

. The same forwhile (someString != "quit")



+6


source







All Articles