visit: allinpython.com for more practicals

C program that will demonstrate the use of strcat( ), strcmp( ), strcpy( ), strlen( ) functions, so before we write program we should know that what is string function? and which string function is used for which task? The answer to those questions is.

What is a string function?

A string is a collection of characters (or collection of letters) that combine to make one or more than one word is called a string and the method use to compare, merge, copy and find the length of the string is called the string function

which string function is used for which task?

The four most used string functions are

  •  strcat( )
  •  strcmp( )
  •  strcpy( )
  •  strlen( )

strcat( ) : this string function is used to merge two string.

strcmp( ): strcmp is used to compare two strings and return the result in an integer.

    •  Basically when 1st string is greater than the 2nd string strcmp return positive value that is 1.
    •  when 1st string is less than the 2nd string strcmp returns negative value that is -1.
    •  when 1st string is equal to 2nd string strcmp return Zero (0).

This three-steps must remember for understand the working of strcmp.

strcpy( ) : this string function is used to copy one string from another.

strlen( ) : strcmp function is used to find a length of string and return value in integer

after understanding uses of string function we write our program that will demonstrate the use of strcat( ), strcmp( ), strcpy( ), strlen( ) functions.

SOURCE CODE:


#include<stdio.h>
#include<string.h>
int main(){
    int i,s;
    char a[20],b[20];
     printf("(demo of strcat):\n");
    printf("enter your name:");
    gets(a);
    printf("enter your surname:");
    gets(b);
    printf("\n");
    
    // demo of strcat()
    printf("\nyour full name is %s\n",strcat(a,b));
     printf("\n");

    // demo of strcmp()
     char c[20],d[20];
    printf("(demo of strcmp):\n");
    printf("enter name1:");
    gets(c);
    printf("enter name2:");
    gets(d);
    i=strcmp(c,d);
    printf("\ncomparation between name1 and name2:%d\n",i);
     printf("\n");

    // demo of strcpy()
      char e[20],f[20];
    printf("(demo of strcpy):\n");
    printf("enter name1:");
    gets(e);
    printf("enter name2:");
    gets(f);
       printf("\ncopy name2 %s\n",strcpy(e,f));

    //  demo of strlen()
       char g[20];
    printf("\n(demo of strlen):\n");
    printf("enter name to get its length:");
    gets(g);
    s=strlen(g);
       printf("(demo of strlen):number of letter in your surname: %d\n",s);

    return 0;
}

OUTPUT:

(demo of strcat):
enter your name:c programming 
enter your surname: language 


your full name is c programming language

(demo of strcmp):
enter name1:language
enter name2:spoken 

comparation between name1 and name2:-1

(demo of strcpy):
enter name1:python
enter name2:c language

copy name2 c language        

(demo of strlen):
enter name to get its length:c programming language
(demo of strlen):number of letter in your surname: 22

C program to sort element in lexicographic order or dictionary order.

C program to accept a string using getchar( ), gets( ), scanf( ), scanf with editset command(^\n).