Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <ctype.h>
- char text[101], character[2];
- int i, j, choice = 0;
- // Ignore extra spaces, count ALL characters
- void showMenu()
- {
- system("cls");
- printf("---- MENU -----\n\n");
- printf("1. Split text\n");
- printf("2. Upper case to lower case\n");
- printf("3. Lower case to upper case\n");
- printf("4. Remove a character\n");
- printf("5. Add a character\n");
- printf("6. Replace a character\n");
- printf("7. Statistics\n");
- printf("8. Exit\n");
- printf("\nYour choice: ");
- }
- void flush()
- {
- int c;
- while ((c = getchar()) != '\n' && c != EOF);
- }
- char enterText()
- {
- flush();
- printf("Enter your text (max 100 chars): ");
- fgets(text, 100, stdin);
- return text;
- }
- void enterCharacter(int n)
- {
- for (i = 0; i < n; i++)
- {
- scanf_s("%c", &character[i]);
- }
- }
- void flush();
- int main()
- {
- while (choice != 8)
- {
- int i = 0;
- fseek(stdin, 0, SEEK_END);
- showMenu();
- scanf_s("%d", &choice);
- printf("\n");
- switch (choice)
- {
- case 1:
- enterText();
- printf("\n");
- while (text[i])
- {
- (text[i] == ' ') ? putchar('\n') : putchar(text[i]);
- i++;
- }
- printf("\n");
- break;
- case 2:
- enterText();
- printf("\n");
- while (text[i])
- {
- putchar(tolower(text[i]));
- i++;
- }
- printf("\n");
- break;
- case 3:
- enterText();
- printf("\n");
- while (text[i])
- {
- putchar(toupper(text[i]));
- i++;
- }
- printf("\n");
- break;
- case 4:
- enterText();
- printf("Enter character to remove: ");
- enterCharacter(1);
- printf("\n");
- while (text[i])
- {
- if (text[i] != character[0])
- putchar(text[i]);
- i++;
- }
- printf("\n");
- break;
- case 5:
- enterText();
- printf("Enter character to add: ");
- enterCharacter(1);
- printf("\n");
- while (text[i])
- {
- switch (text[i])
- {
- case ' ':
- putchar(character[0]);
- putchar(' ');
- break;
- case '\n':
- putchar(character[0]);
- break;
- default:
- putchar(text[i]);
- break;
- }
- i++;
- }
- printf("\n");
- break;
- case 6:
- enterText();
- printf("Enter two characters (ex ab for replacing a with b): ");
- enterCharacter(2);
- printf("\n");
- while (text[i])
- {
- (text[i] == character[0]) ? putchar(character[1]) : putchar(text[i]);
- i++;
- }
- printf("\n");
- break;
- case 7:
- enterText();
- j = 1;
- while (text[i])
- {
- if (text[i] == ' ')
- j++;
- i++;
- }
- printf("\n");
- printf("Number of words: %d\n", j);
- int alphaCount[58] = { 0 };
- for (int i = 0; i < strlen(text); i++)
- if (isalpha(text[i]))
- alphaCount[(text[i]) - 'A']++;
- for (int i = 32; i <= 57; i++)
- (alphaCount[i] != 0) ? printf("%c : %d\n", i + 65, alphaCount[i]) : "";
- for (int i = 0; i <= 25; i++)
- (alphaCount[i] != 0) ? printf("%c : %d\n", i + 65, alphaCount[i]) : "";
- printf("\n");
- break;
- case 8:
- return 0;
- break;
- default:
- printf("Not a valid option!\n\n");
- break;
- }
- system("pause");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement