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