-3

I have previously gotten this to work but when I implemented functions my chars have given me a lot of issues. I have also checked if the strlen is displaying the right number, but it is displaying something weird. If I type in a 10 character long char it will give me 73 (its goes up to 7 then restarts the count, so 14 chars will be 77). The first strncpy works but the other doesn't display anything.

Matthew
  • 5
  • 5
  • You have NOT allocated space for "userInput"... – TonyB Sep 19 '18 at 23:51
  • `strncpy` does **not** NUL-terminate the copy. It os hardly ever what you want, regardless of what you have heard. `strncmp`, however, is useful for comparing a substring with a target. – rici Sep 20 '18 at 00:03
  • 1
    You should look at the man page for "strncpy"... in some cases it does NOT null terminate the destination string. – TonyB Sep 20 '18 at 00:05
  • Additionally, you have NOT completely verified the format of the user-input... For example, what happens if they enter: Palin(1 This statement will cause the "length" calculated by "stringLength - 8" to go negative: strncpy (charsInsideParen, userInput + 6, stringLength - 8); – TonyB Sep 20 '18 at 00:18
  • `sizeof (userInput)` in your GetUserInput function is the size of a pointer, probably 4 or 8, not the length of the allocated memory. – Retired Ninja Sep 20 '18 at 00:24
  • Your "fgets()" call is incorrect... you are saying to read for a length of "sizeof(userInput)" which in GetUserInput() is defined as "char *userInput", which will be 4 or 8 bytes... Perhaps, change the definition of "userInput" in "main()" to be "char userInput[MAX+1]", and change your fgets to be "fgets (userInput, MAX, stdin);" – TonyB Sep 20 '18 at 00:26

1 Answers1

1

Just to get you on the right track, I cleaned up your code.

#include<stdio.h>
#include<string.h>
#define MAX 100

void TestIfPalin(char *palindrome)
{
    // TODO
    printf("TestIfPalin(%s)\n", palindrome);
}

// Given a user-input string of the form "Command(argument)"
// split the string (by inserting \0 characters) into two parts
// and remove the brackets "()" from around the argument
void DetermineWhatCommand(char *userInput)
{
    // look for the left-most '('
    char *left_bracket = strchr(userInput, '(');
    if (left_bracket != NULL)
    {
        // Seperate the command-name from the argument
        *left_bracket = '\0';

        // Look for the right-most ')' in the input
        char *right_bracket = strrchr(left_bracket+1, ')');
        if (right_bracket != NULL)
            *right_bracket = '\0';  // remove the right bracket, it's not needed further
        //else
        //  TODO - error?  No finishing bracket

        // Find the word passed to the function
        char *argument = left_bracket+1;

        if (strcmp(userInput,"Palin") == 0)
            TestIfPalin(argument);
    }
    else
    {
        // No brackets - what sort of command is it?
        // TODO - error message?
    }
 }


char *GetUserInput(char *userInput)
{
    printf("> ");
    fgets(userInput, MAX, stdin);
    userInput[strcspn(userInput, "\n")] = '\0';

    return userInput;
}


int main()
{
    char userInput[MAX] = { '\0' };
    char exitTest[]     = "exit";

    while(strcmp(exitTest, userInput) != 0)
    {
        GetUserInput(userInput);
        if (strcmp(exitTest, userInput) != 0)
            DetermineWhatCommand(userInput);
    }

    return 0;
}

Functions should have a single task. The TestIfPalin() shouldn't be looking for an exit command, nor should getUserInput().

You were trying to read into a non-existant userInput char*. Make sure you understand the difference between char-pointers, and char-arrays. A char-array e.g.:

char userInput[MAX];

Has MAX chars of space. Whereas:

char *userInput;

Has no space, and is just pointing off to wherever (possibly NULL, but that's not guaranteed). It needs to point at something to be used.

char *userInput;
char buffer[MAX];

strcpy(buffer, "bananas");    // OK
strcpy(userInput, "empty!");  // FAILS
userInput = buffer; // userInput is now pointing at buffer
strcpy(userInput, "empty!");  // WORKS (overwrites 'bananas')
Kingsley
  • 14,398
  • 5
  • 31
  • 53
  • @Matthew - I left a comment in the code for this `// TODO - error? No finishing bracket`. Maybe set `userInput` to an invalid command so it doesn't do any further processing? – Kingsley Sep 20 '18 at 02:33