C String
C String
C String
The standard C++ library provides a string class type that supports all the operations mentioned
above, additionally much more functionality. We will study this class in C++ Standard Library
but for now let us check following example:
At this point, you may not understand this example because so far we have not discussed Classes
and Objects. So can have a look and proceed until you have understanding on 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;
However, cin extraction always considers spaces (whitespaces, tabs, new-line...) as terminating the
value being extracted, and thus extracting a string means to always extract a single word, not a
phrase or an entire sentence.
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:
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."
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:
#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:
Table below lists the commonly available library functions for string input and output
Example Output
#include<iostream>
using namespace std;
int main( )
Enter a string for message1:
{char message1[80];
Good morning
char *message2;
Enter a string for message2:
cout <<"Enter a string for message1: \n";
have a nice day
cin.getline(message1,80);
Good morning and have a nice
cout << "Enter a string for message2: \n";
day
cin.getline(message2,80);
cout <<message1<< " and " <<message2;
}
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.
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 your text book.
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
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