Explain any three string functions. Write a program to check if two matrices are identical or act.

This answer is restricted. Please login to view the answer of this question.

Login Now

The Three string library functions are

1. strlen()

This gives the length of the given string including blank spaces and null character.

Example: Write a program to read any string and then find out its length

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

void main(){

   char st[20];
   int l;

   printf("Enter any string");
   gets(st); //or scanf("%s" st);
   l = strlen(st);
   printf("The length of string is:%d",1);

}

2. strcpy()

This is used to copy the content of one string to another string. It takes two arguments: the first is for the destination string array and the second is for the source string array. The source string is copied to the destination string.

Example: Write a program to read any string and then copy to another string by using strcpy() function

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

void main(){
   char st1[] = "Bhupendra";
   char st2[10];
   strcpy(st2.st1);
   puts(st2);
}

3. strcat()

This is used to concatenate (join) two strings and the resulting string is a single string. It takes two arguments: the first is for the destination string array and the Second is for the source string array. The source string and the destination strings are concatenated and the resulting string is stored in the first destination.

Example: Write a program to concatenate any two strings together by using string strcat() function

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

void main(){

    char str1[] = "Welcome ";
    char str2[] = "HamroCSIT";

    strcat(str1, str2);
    puts(str1);

}

Two matrices are said to be identical if and only if they satisfy the following conditions:

  1. Both matrices have the same number of rows and columns.
  2. Both matrices have the same corresponding elements.
#include <stdio.h>
#define N 4

int areSame(int A[][N], int B[][N])
{
    int i, j;
    for (i = 0; i < N; i++)
        for (j = 0; j < N; j++)
            if (A[i][j] != B[i][j])
                return false;
    return true;
}

int main()
{
    int A[N][N] = { {1, 1, 1, 1},
                    {2, 2, 2, 2},
                    {3, 3, 3, 3},
                    {4, 4, 4, 4}};

    int B[N][N] = { {1, 1, 1, 1},
                    {2, 2, 2, 2},
                    {3, 3, 3, 3},
                    {4, 4, 4, 4}};

    if (areSame(A, B))
        printf("Matrices are identical");
    else
        printf("Matrices are not identical");
    return 0;
}

Output

Matrices are identical
If you found any type of error on the answer then please mention on the comment or report an answer or submit your new answer.
Leave your Answer:

Click here to submit your answer.

Discussion
0 Comments
  Loading . . .