C String Function
C String Function
Syntax: strlen(s1);
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[10];
int l;
gets(s1);
l=strlen();
printf(“%d”,l);
getch();
}
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.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[10];
gets(s1);
strlupr(s1);
puts(s1);
getch();
}
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[10];
gets(s1);
strlwr(s1);
puts(s1);
getch();
}
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[10];
gets(s1);
strlrev(s1);
puts(s1);
getch();
}
It copies the string str2 into string str1, including the end character (terminator
char ‘\0’).
#include <stdio.h>
#include <string.h>
int main()
{
char s1[30] = "string 1";
char s2[30] = "string 2 : I’m gonna copied into s1";
/* this function has copied s2 into s1*/
strcpy(s1,s2);
printf("String s1 is: %s", s1);
return 0;
}
strcpy: strcpy() is a standard library function in C/C++ and is used to copy one string to another.
In C it is present in string.h header file and in C++ it is present in cstring header file.
Syntax:
char* strcpy(char* dest, const char* src);
Paramters: This method accepts following paramters:
filter_none
edit
play_arrow
brightness_4
// C program to illustrate
#include <stdio.h>
#include <string.h>
int main()
char str3[40];
char str4[40];
char str5[] = "GfG";
strcpy(str2, str1);
strcpy(str4, str5);
return 0;
Output:
When you combine two strings, you add the characters of one string to the end of
other string. This process is called concatenation. The strcat() function joins 2 strings
together.
It takes the following form :
strcat (string1, string2)
string1 & string2 are character arrays. When the function strcat is executed string2 is
appended to end of string1. The string at string2 remains unchanged.
#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;
}
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
char dest[50] = "This is an";
char src[50] = " example";
strcat(dest, src);
cout << dest;
return 0;
}
strncat: In C/C++, strncat() is a predefined function used for string handling. string.h is the
header file required for string functions.
This function appends not more than n characters from the string pointed to by src to
the end of the string pointed to by dest plus a terminating Null-character. The initial
character of string(src) overwrites the Null-character present at the end of string(dest).
Thus, length of the string(dest) becomes strlen(dest)+n. But, if the length of the
string(src) is less than n, only the content up to the terminating null-character is copied
and length of the string(dest) becomes strlen(src) + strlen(dest).
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.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[10], s2[10];
gets(s1);
strcpy(s2,s1);
puts(s2);
getch();
}