Monday, July 22, 2019

String in C Language


String in C Language:

string in c language



String:-

Strings are actually one-dimensional array of characters terminated by a null character '\0'. Strings are defined as an array of characters.


Syntax Of string:-


cha str-name[size];


Intializing string:

the string should be intialized in different way.

1]. char str[] = "saumil";
2]. char str[20] = "saumil";
3].char str[] = {'s','a','u','m','i','l'};
4].char str[20]= {'s','a','u','m','i','l'};

String Function:

               1]strlen()
       2]strcpy()
       3]strcmp()
       4]strcat()


1]strlen():-

The strlen() function is  used to calculate length of the string.

syntax:-


strlen(str);

Example:-

#include<stdio.h>
#include<conio.h>
void main()
{
  char a[10];
   int length;
   puts("Enter the string");
   gets(a);
   length = sterlen(a);

}


2].strcpy():-


The strcpy() function is used to cpies a one string to the another string.


Syntax:

strcpy(str1,str2);

Example:-

    #include<stdio.h>
#include<conio.h>
void main()
{
      char a[10],b[10];
      int length;
      puts("Enter the string");
      gets(a);
      strcpy(b,a);
      puts(b);

}



3].strcmp():

The strcmp() function is used to compare the two strings.

Syntax:

strcmp(str1,str2);


4].strcat():-

The strcat() function is used to concate or join the two or more strings as a one string.


syntax:

strcat(str1.str2);


EmoticonEmoticon