1.

Explain any three string function with example.

Answer»

Functions in C++

Some functions that are already available in C++ are called pre defined or built in functions.

In C++, we can create our own functions for a specific job or task, such functions are called user defined functions.

A C++ program must contain a main( ) function. A C++ program may contain many lines of statements(including so many functions) but the execution of the program starts and ends with main( ) function.

Predefined functions 

To invoke a function that requires some data for performing the task, such data is called parameter or argument. Some functions return some value back to the called function.

String functions 

To manipulate string in C++ a header le called string.h must be included.

(a) strlen( )- to find the number of characters in a string(i.e. string length).

Syntax: strlen(string);

Eg- cout<<strlen(“Computer”);It prints 8.

(b) strcpy( )- It is used to copy second string into first string.

Syntax: strcpy(string1 ,string2);

Eg. strcpy(str,”BVM HSS”); cout<<str; It prints BVM HSS.

(c) strcat( )- It is used to concatenate second string into first one.

Syntax: strcat(string1 ,string2)

Eg. strcpy(str1 ,’’Hello”); strcpy(str2 ,” World”); strcat(str1 ,str2);

cout<<str1 ; It displays the concatenated string “Hello World”

(d) strcmp( )- it is used to compare two strings and returns an integer.

Syntax: strcmp (string1,string2)

if it is 0 both strings are equal.

if it is greater than 0(i.e. +ve) string1 is greater than string2

if it is less than 0(i.e. -ve) string2 is greater than string1

Eg.

include

#include using namespace std; int main()

{

char str1 [10],str2[10];

strcpy(str1 ,”Kiran”);

strcpy(str2,”Jobi”);

cout<<strcmp(str1 ,str2);

}

It returns a +ve integer.

(e) strcmpi( )- It is same as strcmp( ) but it is not case sensitive. That means uppercase and lowercase are treated as same.

Eg. “ANDREA” and “Andrea” and “andrea” are the same.

# include

# include

using namespace std;

int main( )

{

char str1 [10],str2[10];

strcpy(str1 ,”Kiran”);

strcpy(str2 ,”KIRAN”);

cout<<strcmpi(str1,str2);

}

It returns 0. That is both are same.



Discussion

No Comment Found