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