Slide # 1

Slide # 1

Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts Read More

Slide # 2

Slide # 2

Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts Read More

Slide # 3

Slide # 3

Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts Read More

Slide # 4

Slide # 4

Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts Read More

Slide # 5

Slide # 5

Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts Read More

Friday, November 12, 2010

C++ -Strings

Strings

We will show just one small part of string, and with more details in rate number 2 in Microsam Academy. Exist difference how is the string defined in C and in C++.

In C the string is presented like rank of characters, which finished with “ \0 ”.






#include <iostream>


using namespace std;


int main()

{

                char str[80];

                cout<<"Enter a string: ";


                cin>>str;

                cout<<"Here is your string: ";

                cout<<str<<endl;

                return 0;


}


The program reads string of keyboard. But? Did you try to insert string joined of few words? What happens then? For example, I insert CODE THIS PC and how result I get CODE???


The cause of is that the operator for inserting  >>  stops with reading of keyboard when it comes on first gap place. One manner for resolving this nearly hard problem is to use one function of C++ library:








gets(array-name);




Is defined in library ( cstdio ). This function reads from the keyboard while you press ENTER.








#include <iostream>

#include <cstdio>

using namespace std;


int main()


{

                char str[80];

                cout<<"Enter a string: ";

                gets(str);

                cout<<"Here is your string: ";


                cout<<str<<endl;

                return 0;

}




Exist few functions for manipulation with strings, but I won’t retain on them, I can just mention them:





strcpy(), strcat(), strlen(), strcmp()

0 comments:

Post a Comment