06CString
06CString
06CString
At this point, you may not understand this example because so far we have not discussed Classes
and Objects. You can have a look and proceed until you have an understanding of Object
Oriented Concepts.
#include <iostream>
#include <string>
int main ()
{
string str1 = "Hello";
string str2 = "World";
string str3;
int len ;
return 0;
}
When the above code is compiled and executed, it produces result something as follows:
str3 : Hello
str1 + str2 : HelloWorld
str3.size() : 10
1 string mystring;
2 cin >> mystring;
To get an entire line from cin, there exists a function, called getline, that takes the stream
(cin) as first argument, and the string variable as second. For example:
1 // cin with strings What's your name? Homer Simpson
2 #include <iostream> Hello Homer Simpson.
3 #include <string> What is your favorite team? The Isotopes
4 using namespace std; I like The Isotopes too!
5
6 int main ()
7 {
8 string mystr;
9 cout << "What's your name? ";
10 getline (cin, mystr);
11 cout << "Hello " << mystr << ".\n";
12 cout << "What is your favorite team? ";
13 getline (cin, mystr);
14 cout << "I like " << mystr << " too!\n";
15 return 0;
16 }
Notice how in both calls to getline, we used the same string identifier (mystr). What the
program does in the second call is simply replace the previous content with the new one that
is introduced.
C-String manipulation
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 following declaration and initialization create a string consisting of the word
"Hello". To hold the null character at the end of the array, the size of the character array
containing the string is one more than the number of characters in the word "Hello."
If you follow the rule of array initialization, then you can write the above statement as follows:
Actually, you 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 us try to print
above-mentioned string:
#include <iostream>
int main ()
{
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
return 0;
}
When the above code is compiled and executed, it produces result something as follows:
strlen(s1);
3
Returns the length of string s1.
strcmp(s1, s2);
4
Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.
strchr(s1, ch);
5
Returns a pointer to the first occurrence of character ch in string s1.
strstr(s1, s2);
6
Returns a pointer to the first occurrence of string s2 in string s1.
#include <iostream>
#include <cstring>
int main ()
{
char str1[10] = "Hello";
char str2[10] = "World";
char str3[10];
int len ;
return 0;
}
When the above code is compiled and executed, it produces result something as follows:
C- string manipulation
A string can be declared and initialized by using a pointer or an array of characters. Using
The table below lists the commonly available library functions for string input and output
Example Output
#include<iostream>
using namespace
std; int main( )
{char message1[80]; Enter a string for message1:
char *message2; Good morning
cout <<"Enter a string for message1: \n"; Enter a string for message2:
cin.getline(message1,80); have a nice day
cout << "Enter a string for message2: \n"; Good morning and have a nice
cin.getline(message2,80); day
cout <<message1<< " and " <<message2;
}
Extensive collections of string-handling functions are included with all C++ compilers. The
common of these are listed below. To call these functions, you need to include the header file
String copy
strcpy(string1,string2) - Copies string2 to string1. String1 needs to have enough space
to store string2. The
strcpy will overwrite string1.
String concatenation
strcat(string1,string2) - concatenates string2 to string1. String1 needs to have
enough space to append
string2.
Example: char message1[80] ="Good morning", message2[80]=" and have a nice day";
cout <<message1<<endl;
strcat(message1,message2); cout
<< message1<<endl; cout <<
message2;
Output
Good morning
Good morning and have a nice day and
have a nice day
String comparison
strcmp(string1, string2) - Compares string1 to string2. Returns a negative integer if
string1<string2, 0 if string1
is equal to string2, and a positive integer if string1 > string2.
Note: When strcmp compare the character c in string1 with the character C in string2. The
character c is greater than the character C because the asscii value of character c is 99 and the
asscii value of character C is only 67. See the Appendix B ASCII character set in the back of
String length
strlen(string1) - Return the length of the string, excluding the null character
Example: char message[80] = "Hello world"; int i;
i = strlen(message);
cout << i << " characters"; Output
11 characters
C++ provides several functions that allow you to test and manipulate character data. The
function prototypes are found in the header file name <ctype.h>. Remember to add the line
#include <ctype.h> in program that use these functions. The table below lists and describes the
character functions. Each function expects one integer argument - the ASCII value of the
character to be tested. Each function returns a non- zero value (true) if the condition tested is
true and 0 (false) if the condition tested is false.
The example below will convert each lowercase character of a string to uppercase character
and vice versa.
Example
#include<iostream>
#include<string>
#include<cctype> using namespace std; int main( )
{ char name[20]; cout<<"Enter your name:\n "; cin.getline(name,20);
for( int i = 0; i < strlen(name) ; i++)
{ if (islower(name[i]) )
//convert to uppercase name[i] = toupper(name[i]); else
//convert to lowercase name[i] = tolower(name[i]);
}
//Display the result
cout << "The conversion is:\n"; cout << name << endl;
}
Output
Enter your name: Phuong D. Nguyen The conversion is: pHUONG d.
nGUYEN
Example
#include<iostream>
#include<string>
#include<cctype> using
namespace std; int main()
{ char *name;
cout<<"Enter your name:\n ";
cin.getline(name,20);
for( int i = 0; i < strlen(name) ; i++)
{ if (islower(*(name + i) ) )
//convert to uppercase
*( name + i) = toupper(*(name + i)); else
//convert to lowercase
*( name + i) = tolower(*(name + i));
}
//Display the result
cout << "The conversion is:\n"; cout <<
name << endl;
}
Output
Enter your name: Phuong D.
Nguyen The conversion is:
pHUONG d. nGUYEN
Write a function that returns the number of digits in a given null-terminated string.
#include<iostream>
#include<cctype>
using namespace std;
int numAlphas(const char* s)
{
int count = 0;
for (int i = 0; s[i] != '\0'; i++)
{
if (isdigit(s[i]))
{
count++;
}
}
return count;
int main()
{
char str[] = "a12bc3d";
cout << numAlphas(str);
int main()
{
char s[15] = "Hello World";
cout << myStrLen(s);
return 0;
}
//--------------------------------------------------------------
int myStrLen(char str[])
{
int i = 0;
while (str[i] != '\0')
i++;
return i;
}
Or
Or
}
// create your own strcpy function
#include <iostream>
using namespace std;
void myStrcpy(char str2[], char str1[]); int
main()
{
char s1[15] = "Hello World";
char s2[30];
myStrcpy(s2, s1);
cout << s2; return
0;
}
//--------------------------------------------------------------
void myStrcpy(char *to, char * from)
{
while (*to = *from)
{
to++;
from++;
}
Or