STRING INPUT
//string1.cpp
#include
<iostream>
using
namespace std;
void
main()
{
const int MAXCHARS = 81;
char sentence[MAXCHARS];
cout << "Please enter a
sentence: "<< endl;
cin >> sentence;
cout << sentence << endl;
}
/*Output
Please
enter a sentence:
The
rain in Spain falls mainly on the plain.
The
Press
any key to continue*/
//string2.cpp
#include
<iostream>
using
namespace std;
void
main()
{
const int MAXCHARS = 81;
char sentence[MAXCHARS];
cout << "Please enter a
sentence: "<< endl;
cin.getline(sentence, MAXCHARS, '\n');
cout << sentence << endl;
}
/*Output
Please
enter a sentence:
The
rain in Spain falls mainly on the plain.
The
rain in Spain falls mainly on the plain.
Press
any key to continue*/
//string3.cpp
#include
<iostream>
using
namespace std;
void
main()
{
const int MAXCHARS = 81;
char sentence[MAXCHARS];
cout << "Please enter a
sentence: "<< endl;
cin.getline(sentence, MAXCHARS);
cout << sentence << endl;
}
/*Output
Please
enter a sentence:
The
rain in Spain falls mainly on the plain.
The
rain in Spain falls mainly on the plain.
Press
any key to continue
*/
//string4.cpp
#include
<iostream>
using
namespace std;
void
main()
{
const int MAXCHARS = 81;
char sentence[MAXCHARS];
cout << "Please enter a
sentence: "<< endl;
cin.getline(sentence, MAXCHARS, 'p');
cout << sentence << endl;
}
/*Output
Please
enter a sentence:
The
rain in Spain falls mainly on the plain.
The
rain in s
Press
any key to continue
*/
//string5.cpp
#include
<iostream>
using
namespace std;
void
main()
{
const int MAXCHARS = 81;
char sentence[MAXCHARS];
cout << "Please enter a
sentence: "<< endl;
cin.getline(sentence, 20, '\n');
cout << sentence << endl;
}
/*Output
Please
enter a sentence:
The
rain in Spain falls mainly on the plain.
The
rain in Spain f
Press
any key to continue
/*
#include <iostream>
#include <cstring>
// required for the string function library
using namespace std;
int main()
{
const int MAXELS = 50;
char string1[MAXELS] =
"Hello";
char string2[MAXELS] =
"Hello there";
int n;
n = strcmp(string1,
string2);
if (n < 0)
cout << string1
<< " is less than " << string2 << endl;
else if (n == 0)
cout << string1
<< " is equal to " << string2 << endl;
else
cout << string1
<< " is greater than " << string2 << endl;
cout << "\nThe
length of the string " << string1 << " is "
<<
strlen(string1) << " characters" << endl;
cout << "The
length of the string " << string2 << " is "
<< strlen(string2) << "
characters" << endl;
strcat(string1,"
there World!");
cout <<
"\nAfter concatenation, string1 contains "
<< "the
string value\n" << string1
<< "\nThe
length of this string is "
<<
strlen(string1) << " characters" << endl;
cout <<
"\nType in a sequence of characters for string2:\n";
cin.getline(string2,
MAXELS);
strcpy(string1, string2);
cout <<
"\nAfter copying string2 to string1, "
<< "the
string value in string1 is:\n" << string2
<< "\nThe
length of this string is "
<<
strlen(string1) << " characters" << endl;
return 0;
}
/*Output
Hello is less than Hello there
The length of the string Hello is 5 characters
The length of the string Hello there is 11 characters
After concatenation, string1 contains the string value
Hello there World!
The length of this string is 18 characters
Type in a sequence of characters for string2:
CSC126 rules
After copying string2 to string1, the string value in string1 is:
CSC126 rules
The length of this string is 12 characters
Press any key to continue */
#include
<iostream>
#include
<cstring> // required for the
string function library
using
namespace std;
int
main()
{
const int MAXELS = 50;
char string1[MAXELS];
char string2[MAXELS];
int n;
strcpy(string1, "cat");
strcpy(string2, "catdog");
n = strcmp(string1, string2);
cout << "string
1\t\tstring2\t\tn" << endl;
cout <<
"-----------------------------------------------------" <<
endl;
cout << string1 << "\t\t\t"
<< string2 << "\t\t" << n<< endl;
strcpy(string1, "hello");
strcpy(string2, "hello ");
n = strcmp(string1, string2);
cout << string1 <<
"\t\t\t" << string2 << "\t\t" <<
n<< endl;
strcpy(string1, "hello");
strcpy(string2, "hello");
n = strcmp(string1, string2);
cout << string1 <<
"\t\t\t" << string2 << "\t\t" <<
n<< endl;
strcpy(string1, "123");
strcpy(string2, "12277");
n = strcmp(string1, string2);
cout << string1 <<
"\t\t\t" << string2 << "\t\t" <<
n<< endl;
strcpy(string1, "Spain");
strcpy(string2, "spain");
n = strcmp(string1, string2);
cout << string1 <<
"\t\t\t" << string2 << "\t\t" <<
n<< endl;
strcpy(string1, "123");
strcpy(string2, "aaa");
n = strcmp(string1, string2);
cout << string1 <<
"\t\t\t" << string2 << "\t\t" <<
n<< endl<<endl;
return 0;
}
/*
string
1 string2 n
-----------------------------------------------------
cat catdog -1
hello hello -1
hello hello 0
123 12277 1
Spain spain -1
123 aaa -1
Press
any key to continue
*/