-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring
170 lines (161 loc) · 3.29 KB
/
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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include<stdio.h>
#include<stdlib.h>
int isDigit(char c)
{
if ( c >= '0' && c <='9') return 1;
else return 0;
}
int isCapital(char c)
{
if ( c >= 'A' && c <='Z') return 1;
else return 0;
}
int isLower(char c)
{
if ( c >= 'a' && c <='z') return 1;
else return 0;
}
int isLetter(char c)
{
if ( isCapital(c) || isLower(c) ) return 1;
else return 0;
}
int valid(char *name)
{
for ( int i = 0; name[i] != '\0'; i++) {
if ( !(isLetter(c) || isDigit(c) )) return 0;
}
return 1;
}
char *reverse(char *s)
{
int i;
for ( i = 0; s[i]!= '\0'; i++) {}
char * result = (char *) malloc(sizeof(char) * i);
i = i-1;
int j;
for ( j = 0; i >=0; i--, j++) {
result[j] = s[i];
}
result[j] = '\0';
return result;
}
void reverse(char *s)
{
int i;
for ( i = 0; s[i] != '\0'; i++) {}
if (i < 2) return;
i = i-1;
int j = 0;
while ( j < i ) {
s[j] = s[i-j];
i--;
j++;
}
}
int compare(char *s1, char *s2)
{
int i;
int j;
for ( i = 0, j=0; s1[i] != '\0' && s2[j] != '\0'; i++, j++) {
if ( s1[i] > s2[j] ) {
return 1;
} else if(s1[i] < s2[j]) {
return -1;
}
}
// one of string reached \0
if ( s1[i] < s2[j] ) return -1;
if ( s1[i] > s2[j] ) return 1;
if ( s1[i] == s2[j] ) return 0; // both \0
}
bool isSymmetric(char *s)
{
int j;
for ( j =0; s[j] != '\0'; j++) {
}
if (j < 2) return true;
int i = 0;
j = j-1;
for ( ; i < j; i++, j--) {
if (s[i] == ' ' || s[i] == ',') {
i++;
continue;
}
if ( s[j] == ' ' || s[j] == ',') {
j--;
continue;
}
if ( s[i] != s[j] ) return false;
}
return true;
}
int main()
{
char *s = "John";
for ( int i = 0; s[i]!='\0'; i++) {}
printf("The lenght of word is %d", i);
int vcount = 0; ccount = 0; wcount;
for ( int i = 0; s[i] != '\0'; i++ ) {
if ( s[i] == 'A' || s[i] == 'E' || s[i] == 'I' || s[i] == 'O' || s[i] == 'U' \
|| s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u') {
vount++;
} else if ( s[i] >= 'A' && s[i] <= 'Z' || s[i] >= 'a' && s[i] <= 'z' ) {
count++;
} else if ( s[i] == ' ' && s[i-1] != ' ') {
wcount++;
}
}
cout << "Total vowel is " << vcount << endl;
cout << "Total conce is " << ccount << endl;
cout << "Total words is " <<< wcount+1 << endl;
return 0;
}
void findDuplicate(char *s)
{
int i;
vector<int> hash(127);
for ( i = 0; s[i] != '\0'; s++) {
hash[s[i]]++;
}
for ( i = 0; i < hash.size(); i++) {
if (hash[i] > 1) {
printf("( %c, %d )", i, hash[i]);
}
}
return;
}
void checkDuplicate(char *s)
{
// the same ideas as hash table, but this time uses bitwise operations
// first find the enough space for hash table
double H = 0;
int i;
for ( i = 0; s[i] != '\0'; i++) {
x = 1;
x = x<< s[i];
if ( H & x ) {
// find the duplicate one
printf("%c is found at least twice\n", s[i]);
} else {
H = H | x;
}
}
}
bool isAnagram(char *s1, char *s2)
{
// use hash table
// use toLower to assume all lower case
int H[127]= {0};
for ( int i = 0; s1[i] != '\0'; i++ ) {
H[s1[i]]++;
}
for ( int i = 0; s2[i] != '\0'; i++) {
H[s2[i]]--;
if ( H[s2[i] < 0 ) return false;
}
for ( int i = 0; i <127; i++) {
if (H[i] != 0 ) return false;
}
return true;
}