Topic :String in C++ Programming: C-Style Character String | String Class

String:

String or String Literals such as " InfoBrother " are actually represented by C++ as a sequence of characters in memory. In other words, a string is simply a character array and can be Manipulated as such.


C-Style Character String:

The C-style Character string originated within the C language and continues to be supported within C++. This string is Actually a one-dimensional array of characters which is terminated by a Null character '\0'. thus a Null-terminated string contains the characters that comprise the string followed by a Null.



The Null Character is represented by the combination "\0" (backslash and zero), or by using the constant "NULL" that is available in <iostream> Library:


If we want to declare a string name Name and initialize it to "infobrother", each of the following statements provides the same result:



char Name[ ]    =   " Infobrother ";
char Name[ ]    = { " Infobrother " };
char Name[ 12 ] =   " Infobrother ";
char Name[ 12 ] = { " Infobrother " };
char Name[ 12 ] = { ' I ' , ' n ' , ' f ' , ' o ' , ' b ' , ' r ' , 
                    ' 0 ' , ' t ' , ' h ' , ' e ' , ' r ' , ' \0 ' };


Each Statement reserves 12 character positions, and store an 'I' at Name[0], 'n' at Name[1], 'f' at Name[2] and so on.



When we store a string, the curly braces are Optional, but when we store individual characters, we must include them.


Following is the Memory Presentation of above defined string in C/C++.


Memory Presentation of String

Actually, we do not Place the Null character at the end of a string constant. The C++ compiler automatically places the '\0' at the end of the string when it initializes the Array. Let's have an simple example to print above-mentioned string:



/*string: InfoBrother*/
 
#include <iostream>
using namespace std;
 
int main ()
{
    char Name[12]= {'i','n','f','b','r','o','t','h','e','r'};
    cout<<Name;
    return 0;
}


String Class: <string.h>

The Standard C++ Library Provides a String Class, that defines many functions to perform Manipulation Operations with C++ strings. Here are few Function that Manipulate Null-terminated strings:



Function Name          Syntax         Purpose
String Copy strcpy( S1 ,   S2 );Copy String S2 to String S1.
String Concatenates Strcat( S1 ,   S2 ); Concatenates String S2 at the end of String S1.
String Length Strlen( S1 ) Returns The Length of String S1.
String Compares Strcmp( S1 ,   S2 ) Compares S1 and S2. Returns 0 if both strings are equal. Returns -1 if ( S1 < S2 ). Returns +1 if ( S1 > S2 )
String to Uppercasestrupr( S1 )It Converts a whole String to Uppercase:
String to Lowercasestrlwr( S1 )It Converts a whole String to Lowercase:
String Characterstrchr( S1 , 'b')Finds out first Occurrence of a Given character ('b') in a string, and print out the string starting from given character.
String Reversestrrev( S1 )It Reverses a whole string:
String Character strchr( S1 , ch )Returns a pointer to the first Occurrence of character ch in string S1.
String Stringstrstr( S1 , S2 ) Returns a Pointer to the first Occurrence of String S2 in String S1.


Let's have an complete Example to Understand all String Function that we discussed Above:



/*String Library: Function Example - InfoBrother */
 
#include<iostream>
#include<string.h> //string library, Required to use string Function. 
using namespace std;
 
main()
{
	char S1[15]= "Welcome to "; //string S1
	char S2[15]= "InfoBrother: "; //String S2
	char S3[20]; 
 
	//copy string S1 to S3:
        strcpy(S3, S1);
        cout<<"1): Strcpy( S3, S1 ): Value of S3 is: "<<S3<<endl;
 
	//concatenates S1 and S2;
	strcat(S1, S2);
        cout<<"\n2): strcat( S1, S2): After Concatenation of S1: "<<S1<<endl;
 
	//total Length of S1 after conatentation:
        cout<<" \n3): Strlen( S1 ): Total Length of String is: "<<strlen(S1)<<endl;
 
	//Compare String S1 and S2:
	int compare=strcmp(S1, S2);
	cout<<"\n4): Strcmp( S1, S2): After Compare, The Result is: "<<compare<<endl;
 
	//Convert String S1 to Uppercase:
        strupr(S1);
        cout<<"\n5) Strupr( S1 ): S1 to Uppercase: "<<S1<<endl;
 
	//Convert String S1 back to Lowercase:
        strlwr(S1);
        cout<<"\n6) Strlwr( S1 ): S1 to Lowercase: "<<S1<<endl;
 
	//Find out first occurrence of 'b'. and print out string after 'b'.
	cout<<"\n7): String After first occurrence of 'b': "<<strchr(S1, 'b')<<endl;
 
	//Reverse String S1. 
	strrev( S1 );
	cout<<"\n8): Reverse String S1: "<<S1;
 
	return 0;
}


getline Function:

As we are using cin to get strings with the extraction Operator (>>) as we did with fundamental data type variables.


cin >> string;


However, cin extraction stops reading as soon as it find any blank space character, so by using cin we are not able to get string, even a "complete user Name". if user will enter his first and second name, then cin can Only get first Name, and stops reading at blank character (space). so this behavior may not be what we want.In order to get entire lines, with space, we can use getline() function, which is more recommendable way to get user input with cin:

The Getline function Need two Arguments, first will be cin, and other will be an Variable to store value. The Syntax for getline function is:


getline(cin, Variable-Name);


Have a look at example to understand the concept of getline() function in C++.



/*getline() Function: InfoBrother*/
 
#include<iostream>
#include<string.h> //string library, Required to use string Function.
using namespace std;
 
main()
{
    string name; //Name having type string:
    cout<<"Enter Your Full Name: ";
 
    getline(cin, name); //getline function.
    cout<<"Nice To Meet you: "<<name;
 
    return 0;
}


The String class simplifies string Handling compared to using character Arrays. A disadvantage to using the String Class is that Errors occur when we assign a long value to a string that previously held a shorter value. so it is reasonable option to choose Never to bother with character arrays for storing string values and to always use the String Class instead.







I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement