Home | | Computer Science 11th std | String manipulation (string.h) - C++ Header Files and Built-in Functions

Chapter: 11th Computer Science : Chapter 11 : Functions

String manipulation (string.h) - C++ Header Files and Built-in Functions

The library string.h (also referred as cstring) has several common functions for dealing with strings stored in arrays of characters.

String manipulation (string.h)

 

The library string.h (also referred as cstring) has several common functions for dealing with strings stored in arrays of characters. The string.h header file to be included before using any string function.

 

1. strcpy()

 

The strcpy() function takes two arguments: target and source. It copies the character string pointed by the source to the memory location pointed by the target. The null terminating character (\0) is also copied.

 

Program 11.6

#include <string.h>

#include <iostream>

using namespace std;

int main()

{

      char source[] = "Computer Science";

      char target[20]="target";

      cout<<"\n String in Source Before Copied :"<<source;

      cout<<"\n String in Target Before Copied :"<<target; strcpy(target,source);

      cout<<"\n String in Target After strcpy function Executed :"<<target; return 0;

}

Output:

String in Source Before Copied :Computer Science

String in Target Before Copied :target

String in Target After strcpy function Executed :Computer Science

 

2. strlen()

 

The strlen() takes a null terminated byte string source as its argument and returns its length. The length does not include the null(\0) character.

Program 11.7

#include <string.h>

#include <iostream>

using namespace std;

int main()

{

      char source[ ] = "Computer Science";

      cout<<"\nGiven String is "<<source<<" its Length is "<<strlen(source);

      return 0;

}

Output:

Given String is Computer Science its Length is 16

 

3. strcmp()

 

The strcmp() function takes two arguments: string1 and string2. It compares the contents of string1 and string2 lexicographically.

 

The strcmp() function returns a:

• Positive value if the first differing character in string1 is greater than the corresponding character in string2. (ASCII values are compared)

• Negative value if the first differing character in string1 is less than the corresponding character in string2.

• 0 if string1 and string2 are equal.

 

Program 11.8

#include <string.h>

#include <iostream>

using namespace std;

int main()

{

      char string1[] = "Computer";

      char string2[] = "Science";

      int result;

      result = strcmp(string1,string2);

      if(result==0)

      {

      cout<<"String1 : "<<string1<<" and String2 : "<<string2 <<"Are Equal";

      }

      if (result<0)

      {

      cout<<"String1 :"<<string1<<" and String2 : "<<string2 <<" Are Not Equal";

      }

}

Output

String1 : Computer and String2 : Science Are Not Equal

 

4. strcat()

 

The strcat() function takes two arguments: target and source. This function appends copy of the character string pointed by the source to the end of string pointed by the target.

 

Program 11.9

#include <string.h>

#include <iostream>

using namespace std;

int main()

{

      char target[50] = "Learning C++ is fun";

      char source[50] = " , easy and Very useful";

      strcat(target, source);

      cout << target ;

      return 0;

}

Output

Learning C++ is fun , easy and Very useful

 

5. strupr()

 

The strupr() function is used to convert the given string into Uppercase letters.

 

Program 11.10

using namespace std;

#include<iostream>

#include<ctype.h>

#include<string.h>

int main()

{

      char str1[50];

      cout<<"\nType any string in Lower case :";

      gets(str1);

      cout<<"\n Converted the Source string “<<str1<<into Upper Case is "<<strupr(str1); return 0;

}

Output:

Type any string in Lower case : computer science

Converted the Source string computer science into Upper Case is COMPUTER SCIENCE

 

6. strlwr()

 

The strlwr() function is used to convert the given string into Lowercase letters.

 

Program 11.11

using namespace std;

#include<iostream>

#include<ctype.h>

#include<string.h>

int main()

{

      char str1[50];

      cout<<"\nType any string in Upper case :";

      gets(str1);

      cout<<"\n Converted the Source string “<<str1<<into Lower Case is "<<strlwr(str1);

}

Output:

Type any string in Upper case : COMPUTER SCIENCE

Converted the Source string COMPUTER SCIENCE into lower Case is computer science


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
11th Computer Science : Chapter 11 : Functions : String manipulation (string.h) - C++ Header Files and Built-in Functions |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.