WebNest
Team/Shahnawaz Sazid/M-11-STRING-OPERATIONS

Repository

M-11-STRING-OPERATIONS

View on GitHub ↗
C0 stars0 forks

README

M-11-STRING-OPERATIONS

  • String copy
  • string contamination
  • string compare

string copy

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

int main()
{
    char a[101], b[101];
    scanf("%s %s", &a, &b);

    int length = strlen(b);

    for (int i = 0; i <= length; i++)
    {
        a[i] = b[i];
    }

    printf("%s %s", a, b);
    return 0;
}
  • string copy builtin function

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

int main()
{
    char a[101], b[101];
    scanf("%s %s", &a, &b);
    strcpy(a,b);
    printf("%s %s", a, b);
    return 0;
}

String Contamination

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

int main()
{
    char a[101], b[101];
    scanf("%s %s", &a, &b);
    int a_length = strlen(a);
    int b_length = strlen(b);

    for (int i = 0; i <= b_length; i++)
    {
        a[i + a_length] = b[i];
    }

    printf("%s %s", a, b);

    return 0;
}
  • builtin function of string con cat
#include <stdio.h>
#include <string.h>

int main()
{
    char a[101], b[101];
    scanf("%s %s", &a, &b);
    strcat(a,b);
    printf("%s %s", a, b);

    return 0;
}

string compare

  • string_lexicographical_compare alt text

alt text

#include <stdio.h>

int main()
{
    char a[101], b[101];
    scanf("%s %s", a, b);

    int i = 0;

    while (1)
    {
        if (a[i] == '\0' && b[i] == '\0')
        {
            printf("Equal");
            break;
        }
        else if (a[i] == '\0')
        {
            printf("A Small");
            break;
        }
        else if (b[i] == '\0')
        {
            printf("B Small");
            break;
        }
        else if (a[i] == b[i])
        {
            i++;
        }

        else if (a[i] < b[i])
        {
            printf("A Small");
            break;
        }
        else if (b[i] < a[i])
        {
            printf("B Small");
            break;
        }
    }

    return 0;
}
  • using built in function
#include <stdio.h>
#include <string.h>

int main()
{
    char a[101], b[101];
    scanf("%s %s", a, b);

    int c = strcmp(a, b);

    if (c < 0)
    {
        printf("A is Small");
    }
    else if (c == 0)
    {
        printf("Equal");
    }
    else if (c > 0)
    {
        printf("B is Small");
    }

    return 0;
}
← Back to profile