3i Infotech Placement: Sample Questions 1171 - 1171 of 1245

Glide to success with Doorsteptutor material for competitive exams : get questions, notes, tests, video lectures and more- for all subjects of your exam.

Question 1171

Edit

Describe in Detail

Essay▾

Write a program which uses functions like strcmp () , strcpy () .

Explanation

Strcmp ()

  • strcmp () functions is mainly used in string comparition.
  • If the first character of two strings are equal, next character of two strings are compared.
  • This continues until the corresponding characters of two strings are different or a null character ‘⧵0’ is reached.
  • It is defined in string. h header file.

Program

  1. #include<stdio.h>
  2. #include<string.h>
  3. intmain()
  4. {
  5.     chara[100],b[100];
  6.     printf(“Enterthefirststring”);
  7.     gets(a);
  8.     printf(“Enterthefirststring”);
  9.     gets(b);
  10.     if(strcmp(a,b)==0)
  11.     printf(“Enteredstringsareequal”);
  12.     else
  13.     printf(“Enteredstringsarenotequal”);
  14.     return0;
  15. }

OUTPUT

Enter the first string

Abcd

Enter the second string

abcd

Entered string are not equal.

Strcpy ()

  • The strcpy () function copies the string pointed by source to the character array destination.
  • strcpy () function copies contents of one string into another string.

Syntax

char ⚹ strcpy (char ⚹ destination, const char ⚹ source) ;

If destination string length is less than source string, entire source string value will not be copied into destination.

Program

  1. #include<stdio.h>
  2. intmain()
  3. {
  4.     chars1[100], s2[100],I;
  5.     printf(“Enterstrings1:”);
  6.     scanf(“%s”, s1);
  7.     for(i=0; s1[i] !=‘⧵0’;i++)
  8.     {
  9.         s2[i]=s1[i]
  10.     }
  11.     s2[i]=’⧵0’;
  12.     printf(“strings2:%s”, s2);
  13.     return0;
  14. }

OUTPUT: S

Enter String s1 abcdefgh

String s2: abcdefgh

Developed by: