You are on page 1of 7

Problem: 01

Code:
#include <stdio.h>
int main()
{
    char str[100];
    printf("Enter a String : ");
    gets(str);
    int length = 0;
    for (int i = 0; str[i] != '\0'; i++)
    {
        length++;
    }
    printf("String Length is = %d", length);
    return 0;
}

Output:
Problem: 02
Code:

#include <stdio.h>
int main()
{
    char str1[100];
    char str2[100];
    int length = 0;
    int i = 0, j = 0;
    printf("Enter a String : ");
    gets(str1);
    for (i = 0; str1[i] != '\0'; i++)
    {
        length++;
    }
    for (i = length - 1; i >= 0; i--)
    {
        str2[j] = str1[i];
        j++;
    }
    puts(str2);
}

Output:

Problem: 03
Code:
#include <stdio.h> 
int main()
{
    int i, j, length = 0;
    char str1[100], str2[100];
    printf("Enter 1st String : ");
    gets(str1);
    printf("Enter 2nd String : ");
    gets(str2);
    for (i = 0; str1[i] != '\0'; i++)
    {
        length++;
    }
    str1[length] = ' ';
    length++;
    for (j = 0; str2[j] != '\0'; j++, length++)
    {
        str1[length] = str2[j];
    }
    str1[length] = '\0';
    puts(str1);
}

Output:

Lab task:
Code 1:

#include <stdio.h>
int main()
{
    char string1[200];
    printf("Enter 1st String : ");
    gets(string1);
    char string2[200];
    printf("Enter 2nd String : ");
    gets(string2);
    int i = 0, j = 0, length = 0;
    for (i = 0; string1[i] != '\0'; i++)
    {
        for (j = 0; string2[j] != '\0'; j++)
        {
            if (string1[i] == string2[j])
            {
                length++;
            }
            else
            {
                length = 0;
            }
        }
    }
    if (length > 0)
    {
        printf("Two strings are same\n");
    }
    else
    {
        printf("Two strings are NOT Same.\n");
    }
}
Output:

Code 2:
#include <stdio.h> 
int main()
{
    char string1[200];
    char string2[200];
    int length = 0;
    int i = 0, j = 0, n;
    printf("Enter a String : ");
    gets(string1);
    for (i = 0; string1[i] != '\0'; i++)
    {
        length++;
    }
    for (i = length - 1; i >= 0; i--)
    {
        string2[length - i - 1] = string1[i];
    }
    for (n = 1, i = 0; i < length; i++)
    {
        if (string2[i] != string1[i])
        {
            n = 0;
        }
    }
    if (n == 1)
        printf("%s is palindrome.", string1);
    else
        printf("%s is not a palindrome", string1);
    return 0;
}

Output:
Discussion:
Problem 01: In this problem we take a string which is a character
type value by gets function. Then we run a function till it get the
null value from the string and in the loop we incremented a value
from which we can know the string length.

Problem 02: In this problem we took a string and run a loop till it
get null value. That’s how we got the length of the first string then
we put the last character of the first string to the first position of
the second string by run a loop till first string length. That’s how
we reversed a string.

Problem 03: In this problem we took 2 string by get function then


we calculate the length of the first string and put a space after that
and add every character in the first string of the second-string
step by step by running a loop.

Task 01: In this problem we took two string as input and check
every character to find out whether the strings are same or not. If
two strings are same then it will print Two strings are same
otherwise it will print Two strings are NOT same.

You might also like