-4

I think I made mistakes in the main function if there any please explain it to me as I am new to command line and functions

#include < stdio.h > 
#include < string.h >
  int palindrome(char string[200]) {
    int i;
    int flag = 0;
    for (i = 0; i < strlen(string); i++) {
      if (string[i] == string[strlen(string) - i - 1])
        flag = 1;
      break;
    }
    if (flag == 1)
      printf("palindrome string");
    else
      printf("not palindrome string");

    return 0;

  }
int main(int argc, char * argv[]) {
  char string[200];
  gets(string);
  (argv[1]) == string;
  return palindrome(argv[1]); 

}
  • 5
    What's `(argv[1]) == string;`? Moreover, your logic is incorrect. – babon Jul 13 '18 at 06:20
  • 2
    [Why the `gets()` function is too dangerous to be used — ever!](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used) – Jonathan Leffler Jul 13 '18 at 06:54
  • 5
    Welcome to Stack Overflow. Please read the [About] and [Ask] pages. You need to take time to write the question in the question, not leaving half the question in the title and half in the question. There are so many issues in the code that it is hard to know where to start — ranging from `#include < stdio.h >` (which should not have spaces before and after `stdio.h`) to a comparison whose result is ignored. Why do you read from the standard input if you're meant to process a palindrome from the command line (or why do you look at the argument list if you need to read the input)? – Jonathan Leffler Jul 13 '18 at 06:57
  • sorry for the spaces in i used an online refractor for submission of problem and didnt checked how it destroyed the code with spaces – Vivek Kumar Singh Jul 13 '18 at 16:44
  • now i got it clearly – Vivek Kumar Singh Jul 13 '18 at 16:45

3 Answers3

1

As pointed in comments, the line (argv[1]) == string does nothing (gcc whould have tell you warning: statement with no effect [-Wunused-value])

Moreover, gets function ust not be used, even for test programs, fgets will do the same job, more securely:

/* assuming string is an array: */
fgets(string, sizeof string, stdin); 

/* when string points on at least `size` bytes: */
fgets(string, size, stdin);

So your corrected main() code could be:

int main(int argc, char * argv[])   
{
  char string[200];
  fgets(string, sizeof string, stdin);

  return palindrome(string); 
}

But, there's a logical problem in the palindrome function, look at:

int flag = 0;
for (i = 0; i < strlen(string); i++) {
    if (string[i] == string[strlen(string) - i - 1])
        flag = 1;
    break;
}

With a break placed here, you will only test for i = 0 (you can add one printf("testing i=%d", i); just before the if to see this.

So every string which which begins and ends whith the same letter will be considered like a palindrome...

And also, you should invert the test. flag should be inited to 1 and set to 0 if two tested letter are different.

Your for loop should looks like:

int flag = 1;
for (i = 0; i < strlen(string); i++) {
    printf("testing %d vs %d\n", i, strlen(string)-i-1);
    if (string[i] != string[strlen(string) - i - 1])
    {
        flag = 0;
        break;
    }
}

To go further, the given code can be improved:

  • there is a lot calls to strlen() function, where a single call would be enough,
  • each letter is tested twice, the for loop exit condition can be better...
Mathieu
  • 8,840
  • 7
  • 32
  • 45
1

it is command line based c program to check for palindrome string and my answer is coming right but i had to supply arguments 2 times in the terminal....

Your program cannot give right results because the for loop used to detect the whether the input string is palindrome or not is not iterating more than once. [check below for more details]

I don't know what exactly do mean by - i had to supply arguments 2 times in the terminal

By looking at your code, I can only say that you must be passing the string (to check whether it is palindrome or not) as command line argument and since your main() has gets() so it stop there and wait for user input and there you must be giving same string which you are passing as command line argument to the program.
Seems that this is what you mean by your statement - i had to supply arguments 2 times...

First of all don't use gets(). It's not safe and moreover it has been obsoleted. Check this.
Instead use fgets(). Check this.

There are two ways to get input-

  • Take it from user at run time (you can use fgets() for this)
  • Pass it as command line argument

Since, in the question abstract, you have mentioned -
it is command line based c program to check for palindrome string and my answer is...
So in my answer, I will be taking input as command line argument.

If you are using gcc compiler, compile program with -Wall -Wextra options and compiler will give a warning message on this statement

(argv[1]) == string;

warning: statement with no effect

In the for loop of function palindrome(), you are breaking the loop in first iteration:

for (i = 0; i < strlen(string); i++) {
  if (string[i] == string[strlen(string) - i - 1])
    flag = 1;
  break;  // <--- will break the loop in first iteration
}

And the palindrome() function ends up reporting the palindrome based on whether the first and last character of input string is same or not.

You should break the loop only when any of character at position i in the first half of string does not match with the character at position string_len - i -1 in the second half of string.
Also, you don't need to iterate the whole string to check whether it is palindrome or not:

for (i = 0; i < strlen(string); i++) {
                ^^^^^^^^^^^^^

You just need to iterate the string to its mid to detect whether the string is palindrome or not.

The return type of strlen() is size_t which is an unsigned integer. So, better to take the loop iterator of same type.

There should not be spaces between the header file name and opening and closing angle bracket <,> :

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

While looking for header file, the compiler include the space also in the name and does not trim the leading and trailing space and will report ' stdio.h ' header file not found error.

Putting these all together, you can do:

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

int palindrome(char *p_string) {
    int flag = 0;
    size_t len = strlen(p_string);

    if (len == 1)
        flag = 1;

    for (size_t i = 0; i < (len/2); i++) {
        if (p_string[i] == p_string[len - i - 1])
            flag = 1;
        else
            break;
    }

    if (flag == 1)
        printf("palindrome string\n");
    else
        printf("not palindrome string\n");

    return 0;
}
int main(int argc, char * argv[]) {
    /* Check for number of expected arguments */
    if (argc != 2) {
        fprintf (stderr, "Invalid number of arguments\nUsage:%s <string>\n", argv[0]);
        return -1;
    }

    palindrome(argv[1]);
    return 0;
}

You can use it like this:

$ ./a.out abc
not palindrome string
$ ./a.out aba
palindrome string
$ ./a.out abccba
palindrome string
H.S.
  • 11,654
  • 2
  • 15
  • 32
-1

I don't see an actual question being asked so I will post my own implementation as well, which actually works since your whole code is a mess. Feel free to ask anything.

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

int isPalindrome(char*);

int main(int argc, char * argv[])
{
    /* Skip filepath (argv[0]); end of command line is marked with NULL. */
    for(int argIndex = 1; argv[argIndex] != NULL; ++argIndex)
    {
        isPalindrome(argv[argIndex])
        ? fprintf(stdout, "%s is a palindrome.\n", argv[argIndex])
        : fprintf(stdout, "%s is NOT a palindrome.\n", argv[argIndex]);
    }

    return 0;
}

int isPalindrome(char* pString)
{
    /* Ignore the terminator. */
    size_t lastIndex = strlen(pString) - 1;
    size_t middle = lastIndex / 2;
    /* Include the middle index in case the string length is an odd number. */
    for (size_t offset = 0; offset <= middle; ++offset) 
    {
        /* No need for flags, stop as soon as you see different letters. */
        if (pString[offset] != pString[lastIndex - offset]) 
        {
            return 0;
        }
    }
    /* Line only reached if all symmetrical letters are not different. */
    return 1;
}

Named the executable 'myNewProgram'. That's my console I/O in Ubuntu. Naturally, the quotation marks are to explicitly form the arguments, lest they be separated by spaces.

/home/don/myNewProgram "TEST" "TET" "" "HUH" "OOMPA LOOMPA" "OOOOO"
TEST is NOT a palindrome.
TET is a palindrome.
 is NOT a palindrome.
HUH is a palindrome.
OOMPA LOOMPA is NOT a palindrome.
OOOOO is a palindrome.

If you're running command prompt on Windows, try

<path to program>/<name of program>.exe "TEST" "TET" "" "HUH" "OOMPA LOOMPA" "OOOOO"