+ 1
How can i write this code in C
Write a C program to check whether a string is palindrome or not. Note That: o Declare an arra called as "str" with 100. o Read the text from the user and You must NOT use the function "scanf". o You must use only while for the any loop. o You can use the string function "strlen" and must NOT use any string function. The output is like below for example : Enter a string : madam madam is a palindrome string Attempt https://code.sololearn.com/c0T50E3QdBZv/?ref=app
6 Réponses
+ 2
Abhay yes there are many ways, scanf() is quite bad for strings if used improperly. For strings it has same problem as gets() if you do not give an input width.
The best one is:
fgets(str, sizeof(str), stdin);
it will not add characters past the string size. But it adds a newline to end of the string which you sometimes have to remove (ex, when using strcmp).
another way is K&R style:
int c, len = 0;
char str[100];
while ((c = getchar()) != '\n' && c != EOF && len < 99)
str[len++] = c;
str[len] = '\0';
Or if you can use other scanf functions but not scanf():
fscanf(stdin, "%99s", s);
But never use gets(). it has no size arguments. It is unsafe, deprecated in C99 and as I believe Martin Taylor pointed out ,it was removed from C11 standard and C++14.
+ 1
But the code doesn't work
0
If we will not use scanf how will we read the input from user ? Lol! Or c has some other function to take input
0
You can use
This one
gets(string_name) instead of
scanf("%s", &string_name)
0
We need to see an attempt from your side.