-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path45.c
More file actions
42 lines (42 loc) · 831 Bytes
/
45.c
File metadata and controls
42 lines (42 loc) · 831 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdio.h>
int main()
{
char str[100];
int length = 0, i, flag = 1; // flag=1 means palindrome initially
// Input string
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
// Remove newline character if fgets used
if (str[0] != '\0')
{
int j = 0;
while (str[j] != '\0' && str[j] != '\n')
{
j++;
}
str[j] = '\0';
}
// Find length manually
while (str[length] != '\0')
{
length++;
}
// Check palindrome by comparing characters
for (i = 0; i < length / 2; i++)
{
if (str[i] != str[length - i - 1])
{
flag = 0; // Not palindrome
break;
}
}
if (flag)
{
printf("The string is a palindrome.\n");
}
else
{
printf("The string is not a palindrome.\n");
}
return 0;
}