assignment-s-14.c ​
Metadata ​
- Author — Amit Dutta
- Last updated — 20 Dec 2025
- License — MIT License (See the LICENSE file for details)
Problem Statement ​
Problem Statement
Write a menu-driven program to perform the following string operations: a. Show address of each character b. Concatenate two strings without using strcat() c. Concatenate two strings using strcat() d. Compare two strings e. Find string length using pointers f. Convert lowercase to uppercase g. Convert uppercase to lowercase h. Count number of vowels i. Reverse the string
Source Code ​
Printing the code
To print this file, open it on GitHub and click Raw before printing, or use the Download Raw button above and print directly from that page.
c
#include <stdio.h>
#include <ctype.h>
#include <string.h>
void address_char(char *);
void concat_manual(char[], char[]);
void concat_strcat(char[], char[]);
int string_cmp(char *, char *);
int string_len(char *);
void ltou(char[]);
void utol(char[]);
int vowel_count(char[]);
void reverse(char[]);
int main()
{
char str1[100], str2[100], choice;
int result_cmp;
printf("Enter first string (Max: 100 character): ");
fgets(str1, sizeof(str1), stdin);
str1[strcspn(str1, "\n")] = '\0';
// this will replace the enter (\n) used in the end to null
printf("Enter second string (Max: 100 character): ");
fgets(str2, sizeof(str2), stdin);
str2[strcspn(str2, "\n")] = '\0';
while (1)
{
printf("\n\n\n ========== MENU ==========\n"
"a. Show address of each character\n"
"b. Concatenate two strings without using strcat()\n"
"c. Concatenate two strings using strcat()\n"
"d. Compare two strings\n"
"e. Find string length using pointers\n"
"f. Convert lowercase to uppercase\n"
"g. Convert uppercase to lowercase\n"
"h. Count number of vowels\n"
"i. Reverse the string\n"
"1. Change input strings\n"
"0. Exit\n");
printf("\nEnter your choice: ");
scanf(" %c", &choice);
choice = tolower(choice);
switch (choice)
{
case '1':
while (getchar() != '\n')
;
printf("Enter first string (Max: 100 character): ");
fgets(str1, sizeof(str1), stdin);
str1[strcspn(str1, "\n")] = '\0';
printf("Enter second string (Max: 100 character): ");
fgets(str2, sizeof(str2), stdin);
str2[strcspn(str2, "\n")] = '\0';
break;
case '0':
printf("\n\n\nExiting program...\n\n");
return 0;
case 'a':
printf("\n --- Show address of each character ---\n");
printf("\nSrting 1: ");
address_char(str1);
printf("\nString 2: ");
address_char(str2);
break;
case 'b':
printf("\n --- Concatenate two strings without using strcat() ---\n");
printf("\nResult: ");
concat_manual(str1, str2);
break;
case 'c':
printf("\n --- Concatenate two strings using strcat() ---\n");
printf("\nResult: ");
concat_strcat(str1, str2);
break;
case 'd':
printf("\n --- Compare two strings ---\n");
result_cmp = string_cmp(str1, str2);
if (result_cmp == 0)
{
printf("\nString 1 = String 2");
break;
}
else if (result_cmp > 0)
{
printf("\nString 1 > String 2");
break;
}
else
{
printf("\nString 1 < String 2");
break;
}
case 'e':
printf("\n --- Find string length using pointers ---\n");
printf("\nLength of the String_1: %d", string_len(str1));
printf("\nLength of the String_2: %d", string_len(str2));
break;
case 'f':
printf("\n --- Convert lowercase to uppercase ---\n");
printf("\nString 1: ");
ltou(str1);
printf("\nString 2: ");
ltou(str2);
break;
case 'g':
printf("\n --- Convert uppercase to lowercase ---\n");
printf("\nString 1: ");
utol(str1);
printf("\nString 2: ");
utol(str2);
break;
case 'h':
printf("\n --- Count number of vowels ---\n");
printf("\nString 1: %d", vowel_count(str1));
printf("\nString 2: %d", vowel_count(str2));
break;
case 'i':
printf("\n --- Reverse the string ---\n");
printf("\nString 1: ");
reverse(str1);
printf("\nString 2: ");
reverse(str2);
break;
default:
printf("\nWrong choice. Try again...\n");
}
}
}
void address_char(char *str)
{
printf("\nCharacter | Memory Address");
printf("\n--------- | --------------");
while (*str != '\0')
{
printf("\n %-6c| %p", *str, (void *)str);
str++;
}
}
void concat_manual(char str1[], char str2[])
{
int size1 = strlen(str1);
int size2 = strlen(str2);
char result[size1 + size2 + 1];
int i = 0, j;
j = 0;
while (str1[j] != '\0')
{
result[i] = str1[j];
i++;
j++;
}
j = 0;
while (str2[j] != '\0')
{
result[i] = str2[j];
i++;
j++;
}
result[i] = '\0';
printf("%s\n", result);
}
void concat_strcat(char str1[], char str2[])
{
int size1 = strlen(str1);
int size2 = strlen(str2);
char result[size1 + size2 + 1];
result[0] = '\0';
strcat(result, str1);
strcat(result, str2);
printf("%s\n", result);
}
int string_cmp(char *str1, char *str2)
{
// written based on strcmp()
while (*str1 != '\0' && (*str1 == *str2))
{
str1++;
str2++;
}
return (*str1 - *str2);
}
int string_len(char *str)
{
char *end = str;
while (*end != '\0')
{
end++;
}
return end - str;
}
void ltou(char str[])
{
int i;
for (i = 0; str[i] != '\0'; i++)
{
printf("%c", toupper(str[i]));
}
}
void utol(char str[])
{
int i;
for (i = 0; str[i] != '\0'; i++)
{
printf("%c", tolower(str[i]));
}
}
int vowel_count(char str[])
{
int i, vowel = 0;
char character;
for (i = 0; str[i] != '\0'; i++)
{
character = tolower(str[i]);
if (character == 'a' || character == 'e' || character == 'i' || character == 'o' || character == 'u')
{
vowel++;
}
}
return vowel;
}
void reverse(char str[])
{
int len = strlen(str);
char result[len + 1];
char *start = result;
char *end = result + len - 1;
char temp;
result[0] = '\0';
strcat(result, str);
while (start < end)
{
temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
printf("%s\n", result);
}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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263