1. Explain any four string functions with example for each.

 ANS.Following are some of the useful string handling functions supported by C.

  1. strlen()
  2. strcpy()
  3. strncpy()
  4. strcat()
  5. strncat()
  6. strcmp()
  7. strncmp()
  8. strcmpi()
  9. strncmpi()

These functions are defined in string.h header file. Hence you need to include this header file whenever you use these string handling functions in your program.
All these functions take either character pointer or character arrays as arguments.

strlen()
strlen() function returns the length of the string. strlen() function returns integer value.
Example:

1

2

3

char *str = “Learn C Online”;

int strLength;

strLength = strlen(str);    //strLength contains the length of the string i.e. 14

  1. strcpy()
    strcpy() function is used to copy one string to another. The Destination_String should be a variable and Source_String can either be a string constant or a variable.
    Syntax:
    strcpy(Destination_String,Source_String);
    Example:
1

2

3

4

char *Destination_String;

char *Source_String = “Learn C Online”;

strcpy(Destination_String,Source_String);

printf(“%s”, Destination_String);

Output:
Learn C Online

  1. strncpy()
    strncpy() is used to copy only the left most n characters from source to destination. The Destination_String should be a variable and Source_String can either be a string constant or a variable.
    Syntax:
    strncpy(Destination_String, Source_String,no_of_characters);

strcat()
strcat() is used to concatenate two strings.
The Destination_String should be a variable and Source_String can either be a string constant or a variable.
Syntax:
strcat(Destination_String, Source_String);
Example:

1

2

3

4

char *Destination_String =”Learn “;

char *Source_String = “C Online”;

strcat(Destination_String, Source_String);

puts( Destination_String);

Output:
Learn C Online

strncat()
strncat() is used to concatenate only the leftmost n characters from source with the destination string.
The Destination_String should be a variable and Source_String can either be a string constant or a variable.
Syntax:
strncat(Destination_String, Source_String,no_of_characters);
Example:

1

2

3

4

char *Destination_String=”Visit “;

char *Source_String = “Learn C Online is a great site”;

strncat(Destination_String, Source_String,14);

puts( Destination_String);

Output:
Visit Learn C Online

strcmp()
strcmp() function is use two compare two strings. strcmp() function does a case sensitive comparison between two strings. The Destination_String and Source_String can either be a string constant or a variable.
Syntax:
int strcmp(string1, string2);
This function returns integer value after comparison.
Value returned is 0 if two strings are equal.
If the first string is alphabetically greater than the second string then, it returns a positive value.
If the first string is alphabetically less than the second string then, it returns a negative value
Example:

1

2

3

4

5

char *string1 = “Learn C Online”;

char *string2 = “Learn C Online”;

int ret;

ret=strcmp(string1, string2);

printf(“%d”,ret);

Output:
0

strncmp()
strncmp() is used to compare only left most ‘n’ characters from the strings.
Syntax:
int strncmp(string1, string2,no_of_chars);
This function returns integer value after comparison.
Value returned is 0 if left most ‘n’ characters of two strings are equal.
If the left most ‘n’ characters of first string is alphabetically greater than the left most ‘n’ characters of second string then, it returns a positive value.
If the left most ‘n’ characters of first string is alphabetically less than the left most ‘n’ characters of second string then, it returns a negative value
Example:

1

2

3

4

5

char *string1 = “Learn C Online is a great site”;

char *string2 = “Learn C Online”;

int ret;

ret=strncmp(string1, string2,7);

printf(“%d”,ret);

Output:
0

strcmpi()
strcmpi() function is use two compare two strings. strcmp() function does a case insensitive comparison between two strings. The Destination_String and Source_String can either be a string constant or a variable.
Syntax:
int strcmpi(string1, string2);
This function returns integer value after comparison.
Example:

1

2

3

4

5

char *string1 = “Learn C Online”;

char *string2 = “LEARN C ONLINE”;

int ret;

ret=strcmpi(string1, string2);

printf(“%d”,ret);

Output:
0

strncmpi()
strncmpi() is used to compare only left most ‘n’ characters from the strings. strncmpi() function does a case insensitive comparison.
Syntax:
int strncmpi(string1, string2,no_of_chars);
This function returns integer value after comparison.
Example:

1

2

3

4

5

char *string1 = “Learn C Online is a great site”;

char *string2 = “LEARN C ONLINE”;

int ret;

ret=strncmpi(string1, string2,7);

printf(“%d”,ret);

 

Output:
0

LEAVE A REPLY

Please enter your comment!
Please enter your name here