In C programming, a string is a sequence of characters terminated with a null character. String is an one dimentional character array. Strings are one-dimensional array of type characters terminated by a null character '\0'. This null character indicates the end of the string. Strings are always enclosed by double quotes. Whereas, character is enclosed by single quotes in C.
char c[ ]="c string";
It is represent in string as follows
char a[5];
Here, we declared a string of 5 characters.
Returns the length of string str.
// c program
// example of strlen() function.
#include<stdio.h>
#include<string.h>
int main()
{
char ch[]={'h', 'e', 'l', 'l', 'o','\0'};
printf("Length of string is: %d", strlen(ch));
return 0;
}
Output-
Length of string is: 5
Copies string str2 into str1.
// C program
// example of strcpy() function.
#include<stdio.h>
#include<string.h>
int main ()
{
char str1[]="Hello Guys!";
char str2[] = "C programming";
strcpy(str2, str1);
printf ("str1: %s\nstr2: %s, str1, str2,);
return 0;
}
Output-
str1: Hello Guys! str2: Hello Guys!
Returns 0 if Str1 and Str2 are the same; less than 0 if Str1 < Str2; greater than 0 if Str1>Str2.
// C program
// example of strcmp() function.
#include<stdio.h>
#include<string.h>
int main()
{
char Str1[] = "C programming";
char Str2[] = "C programming";
// Using strcmp()
int k = strcmp(Str1, Str2);
if (k==0)
printf("Strings are equal");
else
printf("Strings are unequal");
printf("\nValue returned by strcmp() is: %d" , k);
return 0;
}
Output-
Strings are equal Value returned by strcmp() is: 0
Concatenates string str2 onto the end of string str1.
// C program
// example of strcat() function.
#include<stdio.h>
#include<string.h>
int main()
{
// Take any two strings
char str1[50] = "efghi";
char str2[50]= "abcd";
strncat(str2, str1);
// Prints the string
printf("Source string : %s\n", str1);
printf("Destination string : %s", str2);
return 0;
}
Output-
Source string : efghi Destination string : abcdefghi
Reverse the string str1.
// C program
// example of strrev() function.
#include<stdio.h>
#include<string.h>
int main()
{
char str1[50] = "hello";
printf("The given string is =%s\n",str1);
printf("After reversing string is =%s",strrev(str1));
return 0;
}
Output-
The given string is =hello After reversing string is =olleh
Returns a pointer to the first occurrence of character ch in string str1.
// C program
// example of strchr() function.
#include<stdio.h>
#include<string.h>
int main () {
char str1[] = "programming";
char ch = 'g';
char *p;
p = strchr(str1, ch);
printf("String starting from %c is: %s", ch, p);
return 0;
}
Output-
String starting from g is: gramming
Returns a pointer to the first occurrence of string str2 in string str1.
// C program
// example of strstr() function.
#include<stdio.h>
#include<string.h>
int main()
{
// Take any two strings
char str1[] = "learnCprogramming";
char str2[] = "Cpro";
char* p;
// Find first occurrence of str2 in str1
p = strstr(str1, str2);
// Prints the result
if (p) {
printf("String found\n");
printf("First occurrence of string '%s' in '%s' is '%s'", str2, str1, p);
} else
printf("String not found\n");
return 0;
}
Output-
String found First occurrence of string 'Cpro' in 'learnCprogramming' is 'Cprogramming'
#include<stdio.h>
#include<conio.h>
int main () {
char s1[12] = "Hello";
char s2[12] = "World";
char s3[12];
int len ;
/* copy s1 into s3 */
strcpy(s3, s1);
printf("strcpy( s3, s1) : %s\n", s3 );
/* concatenates s2 and s3 */
strcat( s1, s2);
printf("strcat( s1, s2): %s\n", s1 );
/* total lenghth of s1 after concatenation */
len = strlen(s1);
printf("strlen(s1) : %d\n", len );
return 0;
}
Output-
strcpy( s3, s1) : Hello strcat( s1, s2): HelloWorld strlen(s1) : 10