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.
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.
#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;
}
String in Source Before Copied :Computer Science
String in Target Before Copied :target
String in Target After strcpy function Executed :Computer Science
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.
#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;
}
Given String is Computer Science its Length is 16
The strcmp() function takes two arguments: string1 and string2. It compares the contents of string1 and string2 lexicographically.
• 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.
#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";
}
}
String1 : Computer and String2 : Science Are Not Equal
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.
#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;
}
Learning C++ is fun , easy and Very useful
The strupr() function is used to convert the given string into Uppercase letters.
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;
}
Type any string in Lower case : computer science
Converted the Source string computer science into Upper Case is COMPUTER SCIENCE
The strlwr() function is used to convert the given string into Lowercase letters.
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);
}
Type any string in Upper case : COMPUTER SCIENCE
Converted the Source string COMPUTER SCIENCE into lower Case is computer science
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.