Skip to content

Commit bd70b82

Browse files
committed
add : string with functios 10
1 parent d7828b2 commit bd70b82

File tree

2 files changed

+43
-13
lines changed

2 files changed

+43
-13
lines changed

14_string_with_functions/10_is_palindrome.c

+43-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Write a c program to reverse a string using user-defined function.
1+
// Write a function to check whether a given string is palindrome or not
22

33
// Header Files
44
#include <stdio.h>
@@ -10,20 +10,23 @@
1010
// Function Declarations
1111
int strLength(char[]);
1212
char *strReverse(char[]);
13+
char *copyString(char[], char[]);
14+
int compareStrings(char[], char[]);
1315
int isStrPalindrome(char[]);
1416

1517
// Main Function Start
1618
int main()
1719
{
1820
char str[ARRAY_SIZE];
1921

20-
printf("\nEnter Any String (MAX CHARACTERS %d) => ", ARRAY_SIZE - 1);
22+
printf("\nEnter Any String to Check Whether It is Palindrome or Not (MAX CHARACTERS %d) => ", ARRAY_SIZE - 1);
2123
fgets(str, ARRAY_SIZE, stdin); // Input String
2224
str[strcspn(str, "\n")] = '\0'; // Replace '\n' character with '\0' in str
2325

24-
printf("\nString Before Reversing => %s", str);
25-
26-
printf("\nString After Reversing => %s", strReverse(str));
26+
if (isStrPalindrome(str))
27+
printf("\nYes, \"%s\" is Palindrome...", str);
28+
else
29+
printf("\nNo, \"%s\" is not Palindrome...", str);
2730

2831
putch('\n');
2932
getch();
@@ -47,14 +50,10 @@ int strLength(char str[])
4750
// Function to Reverse a String
4851
char *strReverse(char str[])
4952
{
50-
int length = 0;
53+
int length = strLength(str);
5154
char temp;
5255

53-
// Find Length of str
54-
while (str[length])
55-
length++;
56-
57-
// // Reverse copyStr
56+
// Reverse str
5857
int beg = 0, end = length - 1;
5958
while (beg < end)
6059
{
@@ -69,11 +68,42 @@ char *strReverse(char str[])
6968
return str;
7069
}
7170

71+
// Function to Copy One String into Another
72+
char *copyString(char des[], char src[])
73+
{
74+
// Copy str into copy
75+
int i = 0;
76+
for (i = 0; src[i]; i++)
77+
des[i] = src[i];
78+
79+
des[i] = '\0';
80+
81+
return des;
82+
}
83+
84+
// Function to Check Whether a Given String an Alphanumeric String or Not
85+
int compareStrings(char str1[], char str2[])
86+
{
87+
for (int i = 0; str1[i] || str2[i]; i++)
88+
{
89+
if (str1[i] > str2[i])
90+
return str1[i] - str2[i];
91+
else if (str1[i] < str2[i])
92+
return str1[i] - str2[i];
93+
}
94+
95+
return 0;
96+
}
97+
7298
// Function to Check Whether a Given String is Palindrome or Not
7399
int isStrPalindrome(char str[])
74100
{
101+
char copyStr[strLength(str) + 1]; // create a string to copy str
102+
copyString(copyStr, str); // copy str into copyStr
103+
strReverse(copyStr); // reverse copyStr
75104

76-
if (copyStr)
105+
if (compareStrings(copyStr, str))
106+
return 0; // String is not Palindrome
77107

78-
return str;
108+
return 1; // String is Palindrome
79109
}
42.3 KB
Binary file not shown.

0 commit comments

Comments
 (0)