String Operations

Algorithm

Step 1: Start

Step 2: Declare three strings str1, str2, str3 and an integer choice

Step 3: Input str1 and str2 from user

Step 4: Show menu with options for string operations

Step 5: Perform selected operation using switch-case

Step 6: Repeat until user selects exit

Step 7: End

Flowchart

graph TD; A([Start]) --> B[/Input str1 and str2/]; B --> C[Display Menu]; C --> D{Choice}; D -->|Yes| E[Perform the string operation]; D -->|No| C[/Display Menu/]; E --> G[/Display the resultant string/]; G --> H([Stop]); classDef start fill:#00cc44,stroke:#006622,stroke-width:2px,color:#ffffff; classDef stop fill:#ff4444,stroke:#cc0000,stroke-width:2px,color:#ffffff; classDef input_output fill:#ffdd44,stroke:#cc9900,stroke-width:2px,color:#000000; classDef process fill:#4488ff,stroke:#0044cc,stroke-width:2px,color:#ffffff; classDef decision fill:#ff8844,stroke:#cc4400,stroke-width:2px,color:#ffffff; class A start; class H stop; class B,C,G input_output; class E process; class D decision;

Program

#include<stdio.h>
#include<string.h>
#include<ctype.h>

void toUpper(char str[]) {
    for (int i = 0; str[i]; i++) {
        str[i] = toupper(str[i]);
    }
}

void toLower(char str[]) {
    for (int i = 0; str[i]; i++) {
        str[i] = tolower(str[i]);
    }
}

int main() {
    char str1[100], str2[100], str3[200];
    int choice;

    printf("Enter first string: ");
    scanf("%s", str1);

    printf("Enter second string: ");
    scanf("%s", str2);

    do {
        printf("\n--- String Operations Menu ---\n");
        printf("1. Length of Strings\n");
        printf("2. Copy str1 to str2\n");
        printf("3. Concatenate str1 and str2\n");
        printf("4. Compare str1 and str2\n");
        printf("5. Reverse str1\n");
        printf("6. Convert str1 to Uppercase\n");
        printf("7. Convert str1 to Lowercase\n");
        printf("8. Exit\n");
        printf("Enter your choice (1-8): ");
        scanf("%d", &choice);
        getchar();

        switch(choice) {
            case 1:
                printf("Length of str1: %lu\n", strlen(str1));
                printf("Length of str2: %lu\n", strlen(str2));
                break;
            case 2:
                strcpy(str2, str1);
                printf("After copying, str2 = %s\n", str2);
                break;
            case 3:
                strcpy(str3, str1);
                strcat(str3, str2);
                printf("Concatenated string: %s\n", str3);
                break;
            case 4:
                if (strcmp(str1, str2) == 0)
                    printf("Strings are equal.\n");
                else
                    printf("Strings are not equal.\n");
                break;
            case 5: {
                int len = strlen(str1);
                printf("Reversed str1: ");
                for(int i = len - 1; i >= 0; i--) {
                    printf("%c", str1[i]);
                }
                printf("\n");
                break;
            }
            case 6:
                toUpper(str1);
                printf("Uppercase str1: %s\n", str1);
                break;
            case 7:
                toLower(str1);
                printf("Lowercase str1: %s\n", str1);
                break;
            case 8:
                printf("Exiting program.\n");
                break;
            default:
                printf("Invalid choice!\n");
        }
    } while(choice != 8);

    return 0;
}