forked from randerson112358/C-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalindrome.c
More file actions
52 lines (35 loc) · 1.06 KB
/
Copy pathpalindrome.c
File metadata and controls
52 lines (35 loc) · 1.06 KB
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
43
44
45
46
47
48
49
50
51
52
//Description: This program determines if a word or string is a palindrome
/*
Example1:
INPUT: "MOM"
OUTPUT: Yes, MOM is a palindrome because the word reversed is the same
Example2:
INPUT: "TOM"
OUTPUT: No, TOM is NOT a palindrome
*/
#include<stdio.h>
#include<string.h> // strlen()
void isPalindrome(char str[]);
int main(){
isPalindrome("MOM");
isPalindrome("M");
return 0;
}
/*
EX: "MOM" = str['M','O','M'], strlen = 3
if str[0] != str[strlen-1] then the string is not a palindrome
if the above statement is not true (e.g. the characters that we are comparing are the same)
then increase our left most index '0' , and decrease our Right most index 'strlen-1'
Continue this until the left most index >= Right most index
*/
void isPalindrome(char str[]){
int lm = 0;//left most index
int rm = strlen(str) - 1;//right most index
while(rm > lm){
if(str[lm++] != str[rm--]){
printf("No, %s is NOT a palindrome \n", str);
return;
}
}
printf("Yes, %s is a palindrome because the word reversed is the same \n", str);
}