0

I have an argv c program, and it reverses the word, and sees if it's a palindrome. I just want to clean up the output and have it print the original input instead of the reversed one, but since it's argv, I can't seem to figure out how to do it.

int main(int argc, char* argv[])
{
    char *string = (char*)malloc(1000);                                                              
    string[0] = '\0';                                                                                

    for(i = 1; i < argc; i++)                                                                        
    strcat(string, argv[i]);

    for(j = 0; string[ j ]; j++)
    string[j] = tolower(string[ j ]);

    reverse(string);
    printf("Reverse of entered word is \"%s\".\n", string);

    result = is_palindrome(string);

    if ( result == 1)
        printf("\"%s\" is a palindrome.\n", string);
    else
        printf("\"%s\" is not a palindrome.\n", string); 

    system("pause");
}

I left out the rest of the functions, but you can see in the main the printf showing whether it is or isn't a palindrome. Not sure how to get that to equal the original input, and knowing the amount of arguments. Currently it prints the reversed string, when I think it would look better printing the original. I think I'm making this too hard but i'm not sure.

Jens
  • 69,818
  • 15
  • 125
  • 179

3 Answers3

0

The obvious way is reversing the reversed string, another way is passing a single argument/string to the program using double quotes:

./program "a man a plan a canal panama"

instead of

./program a man a plan a canal panama

Then you don't need to loop through the args.

David Ranieri
  • 39,972
  • 7
  • 52
  • 94
  • I'll try to reverse the reversed already, but this is for a class, and the professor will be inputting the strings, so I don't have control over that. – Spritesgud Apr 16 '15 at 16:17
0

UPDATE since OP says the professor will enter the sentence (without quotes!).

This can be done simply using MSVC library functions. Because strdup allocated memory, that clean up is also needed.

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

void fatal(char *msg);

int main(int argc, char *argv[]) {
    int i, argslen = 0;
    char *inp, *rev;
    if (argc < 2)                           // check num args
        fatal("Need a string argument");
    for (i=1; i<argc; i++)                  // figure input length
        argslen += strlen(argv[i]) + 1;     // include spaces and EOS
    inp = malloc(argslen);                  // memory for concatenation
    if (inp == NULL)
        fatal("Memory allocation error");
    strcpy(inp, argv[1]);                   // prime with first arg
    for (i=2; i<argc; i++) {                // concat other args
        strcat(inp, " ");
        strcat(inp, argv[i]);
    }

    rev = strdup(inp);                      // make a copy
    if (rev == NULL)
        fatal("Memory allocation error");
    strrev(rev);                            // reverse the copy
    printf("Testing: \"%s\"\n", inp);
    if (stricmp(inp, rev) == 0)             // case insensitive test
        printf("Is a palindrome\n");
    else
        printf("Not a palindrome\n");
    free (inp);                             // release mine
    free (rev);                             // release library's
    return 0;
}

void fatal(char *msg) {
    printf("%s\n", msg);
    exit (1);
}

Program result:

>test Able was I ere I saw Elba
Testing: "Able was I ere I saw Elba"
Is a palindrome
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
0

I see two questions: The amount of arguments is in argc - see e.g. this question for more details on argc and argv[].

On how to print the original string, you can either print the string before you call reverse(), store the original string in a temporary variable to output later or reverse again before you print it. The third option would look like this:

int main(int argc, char* argv[])
{
    char *string = (char*)malloc(1000);                                                              
    string[0] = '\0';                                                                                

    for(i = 1; i < argc; i++)                                                                        
    strcat(string, argv[i]);

    for(j = 0; string[ j ]; j++)
    string[j] = tolower(string[ j ]);

    reverse(string);
    printf("Reverse of entered word is \"%s\".\n", string);

    result = is_palindrome(string);

    if ( result == 1)
        printf("\"%s\" is a palindrome.\n", reverse(string));
    else
        printf("\"%s\" is not a palindrome.\n", reverse(string)); 

    system("pause");
}
Community
  • 1
  • 1
st.eve
  • 114
  • 1
  • 4