Computer Science, asked by rairakshanda6724, 1 year ago

Solve Explain any 5 String functions with example.

Answers

Answered by mrunal6117
10

String function – strlen

Syntax:

size_t strlen(const char *str)

size_t represents unsigned short

It returns the length of the string without including end character (terminating char ‘\0’).

Example of strlen:

#include <stdio.h>

#include <string.h>

int main()

{

char str1[20] = "BeginnersBook";

printf("Length of string str1: %d", strlen(str1));

return 0;

}

Output:

Length of string str1: 13

strlen vs sizeof

strlen returns you the length of the string stored in array, however sizeof returns the total allocated size assigned to the array. So if I consider the above example again then the following statements would return the below values.

strlen(str1) returned value 13.

sizeof(str1) would return value 20 as the array size is 20 (see the first statement in main function).

C String function – strnlen

Syntax:

size_t strnlen(const char *str, size_t maxlen)

size_t represents unsigned short

It returns length of the string if it is less than the value specified for maxlen (maximum length) otherwise it returns maxlen value.

Example of strnlen:

#include <stdio.h>

#include <string.h>

int main()

{

char str1[20] = "BeginnersBook";

printf("Length of string str1 when maxlen is 30: %d", strnlen(str1, 30));

printf("Length of string str1 when maxlen is 10: %d", strnlen(str1, 10));

return 0;

}

Output:

Length of string str1 when maxlen is 30: 13

Length of string str1 when maxlen is 10: 10

Have you noticed the output of second printf statement, even though the string length was 13 it returned only 10 because the maxlen was 10.

C String function – strcmp

int strcmp(const char *str1, const char *str2)

It compares the two strings and returns an integer value. If both the strings are same (equal) then this function would return 0 otherwise it may return a negative or positive value based on the comparison.

If string1 < string2 OR string1 is a substring of string2 then it would result in a negative value. If string1 > string2 then it would return positive value.

If string1 == string2 then you would get 0(zero) when you use this function for compare strings.

Example of strcmp:

#include <stdio.h>

#include <string.h>

int main()

{

char s1[20] = "BeginnersBook";

char s2[20] = "BeginnersBook.COM";

if (strcmp(s1, s2) ==0)

{

printf("string 1 and string 2 are equal");

}else

{

printf("string 1 and 2 are different");

}

return 0;

}

Output:

string 1 and 2 are different

C String function – strncmp

int strncmp(const char *str1, const char *str2, size_t n)

size_t is for unassigned short

It compares both the string till n characters or in other words it compares first n characters of both the strings.

Example of strncmp:

#include <stdio.h>

#include <string.h>

int main()

{

char s1[20] = "BeginnersBook";

char s2[20] = "BeginnersBook.COM";

/* below it is comparing first 8 characters of s1 and s2*/

if (strncmp(s1, s2, 8) ==0)

{

printf("string 1 and string 2 are equal");

}else

{

printf("string 1 and 2 are different");

}

return 0;

}

Output:

string1 and string 2 are equal

C String function – strcat

char *strcat(char *str1, char *str2)

It concatenates two strings and returns the concatenated string.

Example of strcat:

#include <stdio.h>

#include <string.h>

int main()

{

char s1[10] = "Hello";

char s2[10] = "World";

strcat(s1,s2);

printf("Output string after concatenation: %s", s1);

return 0;

}

Output:

Output string after concatenation: HelloWorld

C String function – strncat

char *strncat(char *str1, char *str2, int n)

It concatenates n characters of str2 to string str1. A terminator char (‘\0’) will always be appended at the end of the concatenated string.

Example of strncat:

#include <stdio.h>

#include <string.h>

int main()

{

char s1[10] = "Hello";

char s2[10] = "World";

strncat(s1,s2, 3);

printf("Concatenation using strncat: %s", s1);

return 0;

}

Output:

Concatenation using strncat: HelloWor

Answered by kadamrawat39
0

Explanation:

एक्सप्लेन एनी फाइव स्ट्रिंग फंक्शन बाय गिविंग एग्जांपल

Similar questions