assignment-s-14.c (6948B)
1 /* 2 Write a menu-driven program to perform the following string operations: 3 a. Show address of each character 4 b. Concatenate two strings without using strcat() 5 c. Concatenate two strings using strcat() 6 d. Compare two strings 7 e. Find string length using pointers 8 f. Convert lowercase to uppercase 9 g. Convert uppercase to lowercase 10 h. Count number of vowels 11 i. Reverse the string 12 */ 13 14 #include <stdio.h> 15 #include <ctype.h> 16 #include <string.h> 17 18 void address_char(char *); 19 void concat_manual(char[], char[]); 20 void concat_strcat(char[], char[]); 21 int string_cmp(char *, char *); 22 int string_len(char *); 23 void ltou(char[]); 24 void utol(char[]); 25 int vowel_count(char[]); 26 void reverse(char[]); 27 28 int main() 29 { 30 char str1[100], str2[100], choice; 31 int result_cmp; 32 33 printf("Enter first string (Max: 100 character): "); 34 fgets(str1, sizeof(str1), stdin); 35 str1[strcspn(str1, "\n")] = '\0'; 36 // this will replace the enter (\n) used in the end to null 37 38 printf("Enter second string (Max: 100 character): "); 39 fgets(str2, sizeof(str2), stdin); 40 str2[strcspn(str2, "\n")] = '\0'; 41 42 while (1) 43 { 44 printf("\n\n\n ========== MENU ==========\n" 45 "a. Show address of each character\n" 46 "b. Concatenate two strings without using strcat()\n" 47 "c. Concatenate two strings using strcat()\n" 48 "d. Compare two strings\n" 49 "e. Find string length using pointers\n" 50 "f. Convert lowercase to uppercase\n" 51 "g. Convert uppercase to lowercase\n" 52 "h. Count number of vowels\n" 53 "i. Reverse the string\n" 54 "1. Change input strings\n" 55 "0. Exit\n"); 56 printf("\nEnter your choice: "); 57 scanf(" %c", &choice); 58 choice = tolower(choice); 59 60 switch (choice) 61 { 62 case '1': 63 while (getchar() != '\n') 64 ; 65 printf("Enter first string (Max: 100 character): "); 66 fgets(str1, sizeof(str1), stdin); 67 str1[strcspn(str1, "\n")] = '\0'; 68 69 printf("Enter second string (Max: 100 character): "); 70 fgets(str2, sizeof(str2), stdin); 71 str2[strcspn(str2, "\n")] = '\0'; 72 73 break; 74 case '0': 75 printf("\n\n\nExiting program...\n\n"); 76 return 0; 77 78 case 'a': 79 printf("\n --- Show address of each character ---\n"); 80 printf("\nSrting 1: "); 81 address_char(str1); 82 printf("\nString 2: "); 83 address_char(str2); 84 break; 85 case 'b': 86 printf("\n --- Concatenate two strings without using strcat() ---\n"); 87 printf("\nResult: "); 88 concat_manual(str1, str2); 89 break; 90 case 'c': 91 printf("\n --- Concatenate two strings using strcat() ---\n"); 92 printf("\nResult: "); 93 concat_strcat(str1, str2); 94 break; 95 case 'd': 96 printf("\n --- Compare two strings ---\n"); 97 result_cmp = string_cmp(str1, str2); 98 if (result_cmp == 0) 99 { 100 printf("\nString 1 = String 2"); 101 break; 102 } 103 else if (result_cmp > 0) 104 { 105 printf("\nString 1 > String 2"); 106 break; 107 } 108 else 109 { 110 printf("\nString 1 < String 2"); 111 break; 112 } 113 case 'e': 114 printf("\n --- Find string length using pointers ---\n"); 115 printf("\nLength of the String_1: %d", string_len(str1)); 116 printf("\nLength of the String_2: %d", string_len(str2)); 117 break; 118 case 'f': 119 printf("\n --- Convert lowercase to uppercase ---\n"); 120 printf("\nString 1: "); 121 ltou(str1); 122 printf("\nString 2: "); 123 ltou(str2); 124 break; 125 case 'g': 126 printf("\n --- Convert uppercase to lowercase ---\n"); 127 printf("\nString 1: "); 128 utol(str1); 129 printf("\nString 2: "); 130 utol(str2); 131 break; 132 case 'h': 133 printf("\n --- Count number of vowels ---\n"); 134 printf("\nString 1: %d", vowel_count(str1)); 135 printf("\nString 2: %d", vowel_count(str2)); 136 break; 137 case 'i': 138 printf("\n --- Reverse the string ---\n"); 139 printf("\nString 1: "); 140 reverse(str1); 141 printf("\nString 2: "); 142 reverse(str2); 143 break; 144 default: 145 printf("\nWrong choice. Try again...\n"); 146 } 147 } 148 } 149 150 void address_char(char *str) 151 { 152 printf("\nCharacter | Memory Address"); 153 printf("\n--------- | --------------"); 154 155 while (*str != '\0') 156 { 157 printf("\n %-6c| %p", *str, (void *)str); 158 str++; 159 } 160 } 161 162 void concat_manual(char str1[], char str2[]) 163 { 164 int size1 = strlen(str1); 165 int size2 = strlen(str2); 166 char result[size1 + size2 + 1]; 167 int i = 0, j; 168 169 j = 0; 170 while (str1[j] != '\0') 171 { 172 result[i] = str1[j]; 173 i++; 174 j++; 175 } 176 177 j = 0; 178 while (str2[j] != '\0') 179 { 180 result[i] = str2[j]; 181 i++; 182 j++; 183 } 184 185 result[i] = '\0'; 186 printf("%s\n", result); 187 } 188 189 void concat_strcat(char str1[], char str2[]) 190 { 191 int size1 = strlen(str1); 192 int size2 = strlen(str2); 193 char result[size1 + size2 + 1]; 194 195 result[0] = '\0'; 196 strcat(result, str1); 197 strcat(result, str2); 198 199 printf("%s\n", result); 200 } 201 202 int string_cmp(char *str1, char *str2) 203 { 204 // written based on strcmp() 205 while (*str1 != '\0' && (*str1 == *str2)) 206 { 207 str1++; 208 str2++; 209 } 210 return (*str1 - *str2); 211 } 212 213 int string_len(char *str) 214 { 215 char *end = str; 216 while (*end != '\0') 217 { 218 end++; 219 } 220 return end - str; 221 } 222 223 void ltou(char str[]) 224 { 225 int i; 226 for (i = 0; str[i] != '\0'; i++) 227 { 228 printf("%c", toupper(str[i])); 229 } 230 } 231 232 void utol(char str[]) 233 { 234 int i; 235 for (i = 0; str[i] != '\0'; i++) 236 { 237 printf("%c", tolower(str[i])); 238 } 239 } 240 241 int vowel_count(char str[]) 242 { 243 int i, vowel = 0; 244 char character; 245 for (i = 0; str[i] != '\0'; i++) 246 { 247 character = tolower(str[i]); 248 if (character == 'a' || character == 'e' || character == 'i' || character == 'o' || character == 'u') 249 { 250 vowel++; 251 } 252 } 253 return vowel; 254 } 255 256 void reverse(char str[]) 257 { 258 int len = strlen(str); 259 char result[len + 1]; 260 char *start = result; 261 char *end = result + len - 1; 262 char temp; 263 264 result[0] = '\0'; 265 strcat(result, str); 266 267 while (start < end) 268 { 269 temp = *start; 270 *start = *end; 271 *end = temp; 272 start++; 273 end--; 274 } 275 printf("%s\n", result); 276 }