The strlen() function in C calculates the length of a given string. The strlen() function is
defined in string.h header file. It doesn’t count the null character ‘\0’.
Syntax of C strlen()
The syntax of strlen() function in C is as follows:
size_t strlen(const char* str);
Parameters
The strlen() function only takes a single parameter.
str: It represents the string variable whose length we have to find.
Return Value
This function returns the integral length of the string passed.
C strlen() Function
Example of C strlen()
The below programs illustrate the strlen() function in C:
// c program to demonstrate
// example of strlen() function.
#include <stdio.h>
#include <string.h>
int main()
{
// defining string
char str[] = "GeeksforGeeks";
// getting length of str using strlen()
int length = strlen(str);
printf("Length of string is : %d", length);
return 0;
}
Output
Length of string is : 13
Important Points about strlen()
The following points should be kept in mind while using strlen():
strlen() does not count the NULL character ‘\0’.
The time complexity of strlen() is O(n), where n is the number of characters in
the string.
Its return type is size_t ( which is generally unsigned int ).
strrev() function in C
The strrev() function is a built-in function in C and is defined in string.h header file. The
strrev() function is used to reverse the given string.
Syntax:
char *strrev(char *str);
Parameter:
str: The given string which is needed to be reversed.
Returns: This function doesn’t return anything but the reversed string is stored in the same
string.
Note: This is a non-standard function that works only with older versions of Microsoft C.
Below programs illustrate the strrev() function in C:
Example 1:-
#include <stdio.h>
#include <string.h>
int main()
{
char str[50] = "geeksforgeeks";
printf("The given string is =%s\n", str);
printf("After reversing string is =%s", strrev(str));
return 0;
}
Output:
The given string is = geeksforgeeks
After reversing string is = skeegrofskeeg
C Program to Manually Reverse a String
We can create our own strrev() function to reverse a string simply using a loop. The below program demonstrates how to
reverse a string in C.
C
// C Program to reverse a string
#include <stdio.h>
#include <string.h>
// function to reverse a string
void strrev(char* str)
{
// if the string is empty
if (!str) {
return;
}
// pointer to start and end at the string
int i = 0;
int j = strlen(str) - 1;
// reversing string
while (i < j) {
char c = str[i];
str[i] = str[j];
str[j] = c;
i++;
j--;
}
}
// driver code
int main(void)
{
char a[] = "hello world";
strrev(a);
printf("%s", a);
return 0;
}
Output
dlrow olleh
strcmp()
This function is used to compare two strings. If the two string match, strcmp() return a value 0
otherwise it return a non-zero value. It compare the strings character by character and the comparison
stops when the end of the string is reached or the corresponding characters in the two string are not
same.
strcmp(s1,s2)
return a value:
<0 when s1<s2
=0 when s1=s2
>0 when s1>s2
The exact value returned in case of dissimilar strings is not defined. We only know
that if s1<s2 then a negative value will be returned and if s1>s2 then a positive
value will be returned.
For example:
/*String comparison… ............................ */
#include <stdio.h>
#include <string.h>
int main() // Use int main() instead of void main()
char str1[10], str2[10];
// Prompt user to enter two strings
printf("Enter two strings: ");
// Use fgets instead of gets for safety
fgets(str1, sizeof(str1), stdin);
fgets(str2, sizeof(str2), stdin);
// Remove the trailing newline character that fgets adds
str1[strcspn(str1, "\n")] = '\0'; // Remove newline from str1
str2[strcspn(str2, "\n")] = '\0'; // Remove newline from str2
// Compare the strings
if (strcmp(str1, str2) == 0) {
printf("Strings are the same\n");
} else {
printf("Strings are different\n");
return 0; // Return 0 to indicate successful execution
}
Output: Enter two strings: hello
hello
Strings are the same
strcpy()
This function is used to copying one string to another string. The function
strcpy(str1,str2) copies str2 to str1 including the NULL character. Here str2 is the
source string and str1 is the destination string.
The old content of the destination string str1 are lost. The function returns a pointer
to destination string str1.
Example:-
#include <stdio.h>
#include <string.h>
int main() // Change void to int
char str1[10], str2[10];
// Taking input from the user
printf("Enter a string: ");
scanf("%s", str2); // Corrected: use str2 directly, no need for &
// Copy str2 to str1
strcpy(str1, str2);
// Display both strings
printf("First string: %s\t\tSecond string: %s\n", str1, str2);
// Assign new values to str1 and str2
strcpy(str1, "Delhi"); // Corrected: use str1 instead of str
strcpy(str2, "Bangalore");
// Display the updated strings
printf("First string: %s\t\tSecond string: %s\n", str1, str2);
return 0; // Return 0 to indicate successful execution
}
Output Example:
Enter a string: Hello
First string: Hello Second string: Hello
First string: Delhi Second string: Bangalore
strcat()
This function is used to append a copy of a string at the end of the other string. Ifthe first string is “”Purva” and
second string is “Belmont” then after using this function the string becomes “PusvaBelmont”. The NULL character
from str1 is moved and str2 is added at the end of str1. The 2nd string str2 remains unaffected.A pointer to the
first string str1 is returned by the function.
Example:-
#include <stdio.h>
#include <string.h>
int main() // Use int main() instead of void main()
char str1[20], str2[20];
// Prompt user to enter two strings
printf("Enter two strings: ");
// Use fgets instead of gets for safer input
fgets(str1, sizeof(str1), stdin);
fgets(str2, sizeof(str2), stdin);
// Remove the trailing newline characters added by fgets
str1[strcspn(str1, "\n")] = '\0'; // Remove newline from str1
str2[strcspn(str2, "\n")] = '\0'; // Remove newline from str2
// Concatenate str2 to str1
strcat(str1, str2);
printf("First string: %s\t Second string: %s\n", str1, str2);
// Concatenate "-one" to str1
strcat(str1, "-one");
printf("Now the first string is: %s\n", str1);
return 0; // Return 0 to indicate successful execution
Output:
Enter two strings: Hello
World
First string: HelloWorld Second string: World
Now the first string is: HelloWorld-one