-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathis_palindrome_reverse_string
49 lines (43 loc) · 1.39 KB
/
is_palindrome_reverse_string
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
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
bool isPalindrome(char* some_string) {
int length = strlen(some_string);
// Compare chars from beginning and end until we reach the middle
for (int i = 0; i < (length / 2); i++) {
if (some_string[i] != some_string[length - i - 1]) {
return false; // Not a palindrome
}
}
// If we made it here withouth returning false, it must be a palindrome
return true;
}
void reverseString(char* some_string) {
int length = strlen(some_string);
// Swap chars from beginning and end until we reach the middle
for (int i = 0; i < (length / 2); i++) {
char temp = some_string[i];
some_string[i] = some_string[length - i - 1];
some_string[length - i - 1] = temp;
}
}
int main()
{
char my_string[20];
// Prompt user for a string
printf("Enter a string: ");
scanf_s("%19s", my_string, (unsigned)sizeof(my_string));
// Check if its a palindrome and print a message
bool p_result = isPalindrome(my_string);
if (p_result) {
printf("The entered string, %s, is a palindrome.\n", my_string);
}
else {
printf("The entered string, %s, is NOT a palindrome.\n", my_string);
}
// Reverse if not a palindrome:
if (!p_result) {
reverseString(my_string);
printf("The reversed string is: %s", my_string);
}
}