Single Program (All Manual String Operations)

#include <stdio.h>
int main() 

{
    char str1[50], str2[50], copy[50], concat[100], rev[50];
    int i, j, len = 0, flag = 0;

    //  1. Reading
    printf("Enter first string: ");
    scanf("%s", str1);

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

    //  2. Writing
    printf("\nFirst String: ");
    for(i = 0; str1[i] != '\0'; i++) 
   {
        printf("%c", str1[i]);
    }

    printf("\nSecond String: ");
    for(i = 0; str2[i] != '\0'; i++) 
   {
        printf("%c", str2[i]);
    }

    //  3. Length
    for(i = 0; str1[i] != '\0'; i++);
    len = i;
    printf("\nLength of first string: %d", len);

    //  4. Copy
    for(i = 0; str1[i] != '\0'; i++) 
   {
        copy[i] = str1[i];
    }
    copy[i] = '\0';
    printf("\nCopied string: %s", copy);

    //  5. Comparison
    for(i = 0; str1[i] != '\0' || str2[i] != '\0'; i++)
   {
        if(str1[i] != str2[i]) 
       {
            flag = 1;
            break;
        }
    }

    if(flag == 0)
        printf("\nStrings are Equal");
    else
        printf("\nStrings are Not Equal");

    //  6. Concatenation
    for(i = 0; str1[i] != '\0'; i++) 
   {
        concat[i] = str1[i];
    }

    for(j = 0; str2[j] != '\0'; j++) 
   {
        concat[i] = str2[j];
        i++;
    }
    concat[i] = '\0';

    printf("\nAfter Concatenation: %s", concat);

    //  7. Reverse
    for(i = 0; copy[i] != '\0'; i++);
    for(i = i - 1, j = 0; i >= 0; i--, j++) 
   {
        rev[j] = copy[i];
    }
    rev[j] = '\0';

    printf("\nReversed string: %s", rev);

    //  8. Uppercase
    for(i = 0; copy[i] != '\0'; i++)
   {
        if(copy[i] >= 'a' && copy[i] <= 'z')
       {
            copy[i] = copy[i] - 32;
        }
    }
    printf("\nUppercase: %s", copy);

    //  9. Lowercase
    for(i = 0; copy[i] != '\0'; i++)
   {
        if(copy[i] >= 'A' && copy[i] <= 'Z') 
       {
            copy[i] = copy[i] + 32;
        }
    }
    printf("\nLowercase: %s", copy);
}

Comments

Popular posts from this blog

Single Program Using All Automatic String Functions

C program for subtraction of two matrices (2D Arrays)

C program for Multiplication of Two matrices