'\0'
). Various functions in the <string.h>
header facilitate string manipulation, including finding the length, copying, comparing, and concatenating strings.
1. strlen()
- Description: Returns the length of the string passed to the function.
- Syntax:
size_t strlen(const char *str);
<code>#include<stdio.h> #include<string.h> int main() { char str[100]; printf("Enter a string: "); gets(str); int length = strlen(str); printf("Length of the string: %d", length); return 0; }</code>2.
strcpy()
- Description: Copies the contents of the second string into the first string.
- Syntax:
char* strcpy(char *destination, const char *source);
<code>#include<stdio.h> #include<string.h> int main() { char source[100], destination[100]; printf("Enter a string: "); gets(source); strcpy(destination, source); printf("Copied string: %s", destination); return 0; }</code>3.
strcmp()
- Description: Compares two strings lexically.
- Syntax:
int strcmp(const char *str1, const char *str2);
<code>#include<stdio.h> #include<string.h> int main() { char str1[100], str2[100]; printf("Enter two strings:\n"); gets(str1); gets(str2); int result = strcmp(str1, str2); if(result == 0) printf("Strings are equal"); else if(result < 0) printf("%s is greater", str2); else printf("%s is greater", str1); return 0; }</code>4.
strcat()
- Description: Concatenates the second string to the end of the first string.
- Syntax:
char* strcat(char *destination, const char *source);
<code>#include<stdio.h> #include<string.h> int main() { char str1[100], str2[100]; printf("Enter two strings:\n"); gets(str1); gets(str2); strcat(str1, str2); printf("Concatenated string: %s", str1); return 0; }</code>
Team Answered question April 13, 2024