-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTugasDasprogPalindrome.c
More file actions
115 lines (95 loc) · 2 KB
/
TugasDasprogPalindrome.c
File metadata and controls
115 lines (95 loc) · 2 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <stdio.h>
#include <string.h>
// Fungsi untuk membalikkan sebuah string
void reverse(char *str)
{
int length = strlen(str);
int i, j;
char temp;
for (i = 0, j = length - 1; i < j; i++, j--)
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
// Fungsi untuk menghapus karakter whitespace dari sebuah string
void removeWhitespace(char *str)
{
int i, j = 0;
int length = strlen(str);
for (i = 0; i < length; i++)
{
if (str[i] != ' ')
{
str[j++] = str[i];
}
}
str[j] = '\0';
}
// Fungsi untuk menentukan apakah sebuah kalimat palindrome atau tidak
int isPalindrome(char *str)
{
int length = strlen(str);
int i;
for (i = 0; i < length / 2; i++)
{
if (str[i] != str[length - 1 - i])
{
return 0; // Bukan palindrome
}
}
return 1; // Palindrome
}
int main()
{
char sentence[100];
printf("Masukkan sebuah kalimat: ");
fgets(sentence, sizeof(sentence), stdin);
// Menghapus karakter newline dari input
int length = strlen(sentence);
if (sentence[length - 1] == '\n')
{
sentence[length - 1] = '\0';
}
removeWhitespace(sentence); // Menghapus whitespace
reverse(sentence); // Membalikkan string
if (isPalindrome(sentence))
{
printf("Kalimat tersebut adalah palindrome.\n");
}
else
{
printf("Kalimat tersebut bukan palindrome.\n");
}
return 0;
}
// program simple
#include <stdio.h>
#include <string.h>
int main()
{
char input[20];
int i, length;
int flag = 0;
printf("Enter a string:");
scanf("%s", input);
length = strlen(input);
for (i = 0; i < length; i++)
{
if (input[i] != input[length - i - 1])
{
flag = 1;
break;
}
}
if (flag)
{
printf("%s is not a palindrome", input);
}
else
{
printf("%s is a palindrome", input);
}
return 0;
}