Sunday 16 August 2015

C language Strings Tutorials

Strings

    A string is a group of characters enclosed in a curly braces or ends with a null character

--> The null character is present at the end of the string character array.

--> The null character is represented by "\0".

--> The ASCII value of null character is zero.

--> The purpose of null character is, it indicates that end of the string.

   Syntax:

      datatype array_name[size];

      Eg: char a[100];

--> Here array name indicates name of the character array and size indicates the number of characters it can store.

      Eg: char a[100];

Initialization:

char a[20] = "LetUsCodeinC";

char a[ ] ={'L','e','t','U','s','C','o','d','e','i','n','C'};

String library functions:

1) strlen( )

      This function is used to find the length of string i.e no. of characters present in it. This function return number of characters in the string expect null character.

   Syntax:

      strlen(string);

Eg:-

#include< stdio.h >
#include< conio.h >
void main( )

{

      char str[20];
      int l;
      printf("\nEnter String: ");
      gets(str);
      l = strlen(str);
      printf("Length of the given string: %d", l);

}

2) strcpy( )

This function is used to copy contents of one string into another string.

   Syntax:

            strcpy(string1, string2)

3) strcat( )

This function is used to concatenate two string values i.e contents of one string are attached with the contents of another string.

   Syntax:

            strcat(string1, string2)

--> Here contents of string2 are appended to the contents of string1.

4) strrev( )

This function is used to reverse the contents of given string.

   Syntax:

            strrev(string);

5) strcmp( )

This function is used to compare two string values whether they are equal, which one is bigger or smaller string.

   Syntax:

            strcmp(string1, string2);

--> It performs comparison on the two strings based on the ASCII value of the characters present in the string.

6) strlwr( )

This function is used to convert uppercase letters in a string into lowercase.

   Syntax:

            strlwr(string);

7) strupr( )

This function is used to convert lowercase letters in a string to uppercase.

   Syntax:

            strupr(string);




0 comments:

Post a Comment