Professional Documents
Culture Documents
OF STRING IN
C++
BY: SABOOH HAROON BUTT
Method 1:
char address[]={'T', 'E', 'X', 'A', 'S', '\0'};
Method 2: The above string can also be definedas –
char address[]="TEXAS";
String functions:
1.Strlen : find out the length of a string
2.Strlwr : it converts a string into lower case
3.Strupr: it converts a string to uppercase
4.Strcat : it appends one string at the end of another
5.Strcpy : use it for copying a string into another
6.Strcmp : it compares two stringd
7.Strncmp : it compare first character of two strings
8.Strcmpi : it compare two strings without regard to case (“I” denotes that this function is ignores case
9.Stricmp: it compare first n character of two strings, its not case sensitive
10.
Strdup: used for duplicating a string
11.
Strchr: finds out first occurrence of a given character in a string
12.
Strrchr: find out last occurrence of a given character in a string
13.
Strstr: find first occurrence of a given string in another string
14.
Strset: it sets all character of a string to a give character
15.
Strrev: it reverse a string
16.
Strnset: it sets first n character of a string to a given charact er
06/20/2021 3
IMPORTANT FUNCTION OF STRING
function – strlen
Syntax:
size_t strlen(const char *str)size_t
it represents unsigned short
It returns the length of the string without including end character (terminating
char ‘\0’).
Example of strlen:
#include <iostream.h>
#include <string.h>
int main()
{ char str1[20] = saboohharoon";
cout<<“length of a string :”,s;
strlen(str1));
return 0; }
Output:
Length of string str1:12
PRGRAMMING FUNDAMENTAL 06/20/2021 4
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";
cout<<"Length of string str1 when maxlen is 30:",s, strnlen(str1, 30); printf("Length of
string str1 when maxlen is 10:”,s, strnlen (str1, 10);
}
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
PRGRAMMING FUNDAMENTAL 06/20/2021 5
C String function – strcmp
int strcmp(const char *str1, const char *str2)
IIf 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 <iostream.h>
#include <string.h>
int main()
{
char s1[20] = “hello";
char s2[20] = “helloworld”;
if (strcmp(s1, s2) ==0)
{
Cout<<"string 1 and string 2 are equal";
}
else {
cout<<"string 1 and 2 are different“;
}
return 0; }
Output
StringPRGRAMMING
1 and stringFUNDAMENTAL
2 are different 06/20/2021 6
C String function – strcat
char *strcat(char *str1, char *str2)
It concatenates two strings and returns the concatenated string.
Example of strcat:
#include <iostream.h>
#include <string.h>
int main()
{ char s1[10] = "Hello";
char s2[10] = "World";
strcat(s1,s2);
Cout<<“output string after concatenation: ", s1);
return 0;
}
Output:
Output string after concatenation: HelloWorld 06/20/2021
PRGRAMMING FUNDAMENTAL 7
C String function – strcpy