+ 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

18th May 2020, 2:19 PM
Ahmed Mulki
Ahmed Mulki - avatar
6 odpowiedzi
+ 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.
18th May 2020, 5:03 PM
Gen2oo
Gen2oo - avatar
18th May 2020, 2:59 PM
Ahmed Mulki
Ahmed Mulki - avatar
+ 1
But the code doesn't work
18th May 2020, 3:08 PM
Ahmed Mulki
Ahmed Mulki - avatar
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
18th May 2020, 2:24 PM
Abhay
Abhay - avatar
0
You can use This one gets(string_name) instead of scanf("%s", &string_name)
18th May 2020, 2:30 PM
Ahmed Mulki
Ahmed Mulki - avatar
0
We need to see an attempt from your side.
18th May 2020, 2:46 PM
HonFu
HonFu - avatar