You are on page 1of 9

FUNCTIONS

OF STRING IN
C++
BY: SABOOH HAROON BUTT

PRGRAMMING FUNDAMENTAL 06/20/2021 1


STRINGS
String Declaration
STRING DECLARACTION
1. Char str1[]={ ‘A’, ’B’, ‘C’, ‘D’,’\0’};
2. char str1[]=“ABCD”;

Method 1:
char address[]={'T', 'E', 'X', 'A', 'S', '\0'};
Method 2: The above string can also be definedas –
char address[]="TEXAS";

PRGRAMMING FUNDAMENTAL 06/20/2021 2


In the above declaration NULL character (\0) will automatically be inserted at the end of the string.
What is NULL Char “\0”?
'\0' represents the end of the string. It is also referred as String terminator & Null Character.

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

char *strcpy( char *str1, char *str2)


It copies the string str2 into string str1, including the end character (terminator char
‘\0’).
Example of strcpy:
#include <iostream.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);
Cout<<"String s1 is: ", s1);
return 0; }
Output:
String s1 is: string 2: I’m gonna copied into s1

PRGRAMMING FUNDAMENTAL 06/20/2021 8


Strrev funtion in string
Reverse of string
#include<iostream.h>
#include<string.h>
Int main()
{
Char a[13]=“CLASS”;
Char b[12]= “eeeccc”;
Cout<<strrev(a);
output:
SSALC
Strlwr function in string
Convert into lowercase
If we apply this function to the above mention program then it will convert the output as
Cout<<strlwr(a);
Output:
Class
Strupr function in string
Convert into upper case
Cout<<strupr(b);
Output
EEECCC.
PRGRAMMING FUNDAMENTAL 06/20/2021 9

You might also like