-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_operations
More file actions
161 lines (144 loc) · 3.01 KB
/
string_operations
File metadata and controls
161 lines (144 loc) · 3.01 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
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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
int i = 0, j = 0, k=0;
char c;
int length(char str[]);
void reverse(char str[]);
//void search(char str[]);
void copy(char str2[], char str1[]);
int compare(char str1[], char str2[]);
void concat(char str1[], char str2[]);
int palindrome(char str[]);
int frequency(char str[]);
int main(){
char a[15], b[15];
int res, ch;
do{
printf("\n1. Length of a string");
printf("\n2. Reverse a string");
printf("\n3. Copy string");
//printf("\n4. Search in a string");
printf("\n4. Check if a string is palindrome");
printf("\n5. Compare two strings");
//printf("\n7. Compare two strings");
printf("\n6. Count frequency of a character");
printf("\n7. Exit");
printf("\n. Enter your choice");
scanf("%d", &ch);
//flushall();
switch(ch){
case 1:
printf("\nEnter the string");
scanf("%s", a);
res = length(a);
printf("\nLength of the string is %d", res);
break;
case 2:
printf("\nEnter the string");
scanf("%s", a);
reverse(a);
printf("\nReversed string is %s", a);
break;
case 3: copy(b,a); //copy a in b
printf("\nNew string b is %s", b);
break;
case 4: printf("\nEnter the string");
scanf("%s", a);
res = palindrome(a);
if(res == 0)
printf("\nNot palindrome");
else
printf("\nPalindrome");
break;
case 5: printf("\nEnter first string");
scanf("%s", a);
printf("\nEnter second string");
scanf("%s", b);
res = compare(a,b);
if(res == 0)
printf("\nBoth are same");
else if(res > 0)
printf("\nstring a > string b");
else
printf("\nstring b > string a");
break;
case 6:
printf("\nEnter the string");
scanf("%s", a);
res = frequency(a);
printf("\n%c occured %d times", c, res);
break;
default: printf("\nInvalid entry!");
}
}while(ch !=7);
return 0;
}
int length(char str[]){
while(str[i] != '\0')
i++;
return i;
}
void reverse(char str[]){
char temp;
i = j = 0;
while(str[i] != '\0')
i++;
//i now contains length of the string
i--; //as indices start from 0
while(j<i){
temp = str[j];
str[j] = str[i]; // swap last and first value
str[i] = temp;
j++;
i--;
}
}
int palindrome(char str[]){
while(str[j]!= '\0')
j++;
i =0;
while(i<j){
if(str[i] != str[j-1])
return 0;
i++; //move forward
j--; // come backward
}
return 1;
}
void copy(char str2[], char str1[]){
while(str1[i] != '\0'){
str2[i] = str1[i];
i++;
}
str2[i] = '\0';
}
int compare(char str1[], char str2[]){
i=0;
while(str1[i] != '\0'){
if(str1[i] > str2[i])
return 1;
if(str1[i] < str2[i])
return -1;
}
return 0;
}
void concat(char str1[], char str2[]){
while(str1[i] != '\0')
i++;
for(j=0; str2[j]!= '\0'; i++,j++)
str1[i] = str2[j];
str1[k] = '\0';
}
int frequency(char str[]){
printf("\nEnter the character");
scanf("%c", &c);
int count = 0;
for(i = 0; str[i] != '\0'; ++i)
{
if(str[i] == 'c')
++count;
}
return count;
}