String Functions in C Language(C Language)

 

C String Functions

There are many important string functions defined in "string.h" library.

No.

Function

Description

1)

strlen(string_name)

returns the length of string name.

2)

strcpy(destination, source)

copies the contents of source string to destination string.

3)

strcat(first_string, second_string)

concats or joins first string with second string. The result of the string is stored in first string.

4)

strcmp(first_string, second_string)

compares the first string with second string. If both strings are same, it returns 0.

5)

strrev(string)

returns reverse string.

6)

strlwr(string)

returns string characters in lowercase.

7)

strupr(string)

returns string characters in uppercase.

8)

strstr(string,string)

find the first occurrence of a substring within a given string.

9)

strchr()

This function locates the first occurrence of a character in a string.

10)

strrchr()

This function locates the last occurrence of a character in a string

  C String Length: strlen() function

The string length function returns the length of the given string. It doesn't count null character '\0'.

Syntax:

strlen(string_name);

Example:

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main() 

{

    char a[10];

    clrscr();

    printf("Enter a string");

    gets(a);

    printf("The length of the string = %d",strlen(a));

    getch();

}

Output:

Enter a string example

The length of the string = 7

C Copy String: strcpy()

The String copy function copies the source string in destination.

Syntax:

strcpy(destination, source);

Example:

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main() 

{

    char a[10],b[10];

    clrscr();

    printf("Enter a string");

    gets(a);

    printf("The destination string = %s",strcpy(b,a));

    getch();

}

Output:

Enter a string example

The destination string = example

Strcat() function in C

This function accepts two input strings and appends the second string's content to the end of the first string.

Syntax:

strcat(first_string, second_string);

Example:

#include <stdio.h>

#include<conio.h>

#include<string.h>

void main() 

{

    char a[10],b[10];

    clrscr();

    printf("Enter first string ");

    gets(a);

    printf("Enter second string ");

    gets(b);

    printf("Concatination of two strings = %s",strcat(a,b));

    getch();

}

Output:

Enter first string hello

Enter second string world

Concatination of two strings = helloworld

Strcmp() function in C

In C programming, it is frequently used to compare two strings

Ø  If str1 is lexicographically superior to str2, it returns an integer number larger than 0.

Ø  If str1 is lexicographically smaller to str2, it returns an integer value that is less than 0.

Ø  It returns 0 istr1 and str2 are equal.

Syntax:

strcmp(first_string, second_string)

Example:

#include <stdio.h>

#include<conio.h>

#include<string.h>

void main() 

{

    char a[10],b[10];

    clrscr();

    printf("Enter first string ");

    gets(a);

    printf("Enter second string ");

    gets(b);

    printf("Comparision of two strings = %s",strcmp(a,b));

    getch();

}

Output:

Enter first string Example

Enter second string Example

Comparision of two strings = 0

C Reverse String: strrev()

The strrev(string) function returns reverse of the given string. Let's see a simple example of strrev() function.

Syntax:

strrev(string);

Example:

#include <stdio.h>

#include<conio.h>

#include<string.h>

void main() 

{

    char a[10];

    clrscr();

    printf("Enter a string ");

    gets(a);

    printf("The given string is ");

    puts(a);

    printf("Reverse of a given string is %s",strrev(a));

    getch();

}

Output:

Enter a string hello

The given string is hello

Reverse of a given string olleh

Strlwr() function in C

The strlwr(string) function returns string characters in lowercase. Let's see a simple example of strlwr() function.

Syntax:

strlow(string);

Example:

#include <stdio.h>

#include<conio.h>

#include<string.h>

void main() 

{

    char a[10];

    clrscr();

    printf("Enter a string ");

    gets(a);

    printf("The given string is ");

    puts(a);

    printf("Lower case of a given string is %s",strlow(a));

    getch();

}

Output:

Enter a string HELLO

The given string is HELLO

Reverse of a given string hello

C String Uppercase: strupr()

The strupr(string) function returns string characters in uppercase. Let's see a simple example of strupr() function.

Syntax:

strupr(string);

Example:

#include <stdio.h>

#include<conio.h>

#include<string.h>

void main() 

{

    char a[10];

    clrscr();

    printf("Enter a string ");

    gets(a);

    printf("The given string is ");

    puts(a);

    printf("Upper case of a given string is %s",strupr(a));

    getch();

}

Output:

Enter a string hello

The given string is hello

Reverse of a given string HELLO

strstr() function in C

it is used to find the first occurrence of a substring within a given string. If the substring is found in the string, it returns a pointer to the first character of the first occurrence of the substring. The function returns a NULL pointer if the substring is not present in the given string.

Syntax:

strstr(string,string)

Example:

Comments

Popular posts from this blog

PHP Array Functions

Object Instance Working with Strings