DOC

# Exercise 6

By Eric Rose,2015-04-06 00:50
6 views 0
Exercise 6Exerci

Chapter Six Strings

Exercises

1. What is the output from the following program segment?

char str1[] = "abc" ;

char str2[] = "ABCD" ;

cout << str1 << endl << strlen( str1 ) << endl ; if ( strcmp( str1, str2 ) == 0 )

cout << str1 << "==" << str2 << endl ; else

if ( strcmp( str1, str2 ) < 0 )

cout << str1 << "<" << str2 << endl ; else

if ( strcmp( str1, str2 ) > 0 )

cout << str1 << ">" << str2 << endl ; char str3[8] ;

strcpy( str3, str1 ) ;

strcat ( str3, str2 ) ;

cout << str3 << endl << strlen( str3 ) << endl ; str3[6] = 'x' ;

cout << str3 << endl ;

output as follows

abc

3

abc>ABCD

abcABCD

7

abcABCx

2. Modify exercise 1 to use C++ strings rather than C-strings.

string str1 = "abc" ;

string str2 = "ABCD" ;

cout << str1 << endl << str1.length() << endl ;

if ( str1== str2 )

cout << str1 << "==" << str2 << endl ;

else

if ( str1 < str2)

cout << str1 << "<" << str2 << endl ;

else

if ( str1 > str2 )

cout << str1 << ">" << str2 << endl ;

string str3 ;

str3 = str1;

str3 = str3 + str2;

cout << str3 << endl << str3.length()<< endl ;

str3.at(6) = 'x' ;

cout << str3 << endl ;

3. Given the following,

char c_str1[18] ;

char c_str2[6] = "abcde" ;

what is in c_str1 after each of the following?

(a) strcpy( c_str1, "A string" ) ;

(b) strcat( c_str1, " of text." ) ;

c) strncpy( c_str1, c_str2, 1 ) ; c_str1 is:

A string.

A string of text.

a string of text.

4. Write a program to input a C-string from the keyboard and replace each space in the string

with the character '_'.

#include

#include

using namespace std;

int main ( )

{

string output="";

char ch;

while((ch=getchar())!='\n')

{

if(ch != ' ')

output = output + ch;

else

output = output + '_';

}

cout << output << endl;

return 0 ;

}

5. What is the output from the following program segment?

string str = "ABCDEFGHIJ" ;

cout << str << endl << str.length() << endl ;

str.replace( 4, 2, "123456" ) ;

str.at( 3 ) = '0' ;

cout << str << endl << str.length() << endl ;

str.erase( 10, 2 ) ;

cout << str << endl << str.length() << endl ;

cout << str.substr( 3, 7 ) << endl ;

str += "KLMN" ;

cout << str << endl << str.length() << endl ;

str.insert( 10, "7890" ) ;

cout << str << endl << str.length() << endl ;

cout << str.find( "0" ) << endl ;

output as follows:

ABCDEFGHIJ

10

ABC0123456GHIJ

14

ABC0123456IJ

12

0123456

ABC0123456IJKLMN

16

ABC01234567890IJKLMN

20

3

6. Read in three names from the keyboard and display them in alphabetical order

该程序使用冒泡排序法

#include

#include

using namespace std;

#define COUNT 3 //定义数组大小

int main ( )

{

int i;

string name[COUNT]; //定义一个字符串数组

for(i=0; i

cin >>name[i];

for(i=0; i

{

for(int j=COUNT-1; j>i; j--) //每次都求出当前最小的字符串

{

if(name[j] < name[j-1]) //当前字符串比它前一个字符串小交换位置

{ //以保证前面的字符串是最小的

string temp = name[j];

name[j] = name[j-1];

name[j-1] = temp;

}

}

}

for(i=0; i

cout << name[i] << endl;

return 0 ;

}

7. Write a program to read in a line of text from the keyboard and calculate the average length

of the words in that line. Assume each word in the line is separated from the next by at least one

space. Allow for punctuation marks. Use C++ strings.

#include

#include

using namespace std;

int main ( )

{

int i=0;

string text = ""; //定义一个字符串数组

char ch;

while((ch=getchar())!='\n') //接收文本

text = text + ch;

int sum = 0; //有效字符长度和

int words = 0; //词的个数

while(i

{

int start = i; //记录一个单词的开始位置

while(i

i++;

if(i != start)

start = i;

while(i

i++; //单词的结束位置

if(i != start)

{

words ++;

sum = sum + i - start;

}

}

cout << sum << endl;

cout << words << endl;

cout << sum*1.0/words << endl;

return 0 ;

}

8. Modify exercise 7 to display the number of words in the line with lengths of

(a) 1

(b) 2 to 5

(c) 6 to 10

(d) 11 to 20

(e) 21 and above.

#include

#include

using namespace std;

#define CLUSTER 5

int main ( )

{

int i=0;

string text = ""; //定义一个字符串数组

char ch;

while((ch=getchar())!='\n') //接收文本

text = text + ch;

int sum = 0; //有效字符长度和

int words = 0; //词的个数

int hist[CLUSTER]={0}; //要求区域个数

while(i

{

int start = i; //记录一个单词的开始位置

while(i

i++;

if(i != start)

start = i;

while(i

i++; //单词的结束位置

if(i != start)

{

if((i-start) ==1)

hist[0]++;

else if((i-start) >=2 && (i-start) <=5)

hist[1]++;

else if((i-start) >=6 && (i-start) <=10)

hist[2]++;

else if((i-start) >=11 && (i-start) <=20)

hist[3]++;

else

hist[4]++;

}

}

for(i=0; i

cout << hist[i] << endl;

return 0 ;

}

9. Write a program to ask a user for their name. The user’s name is then compared with a list of names

held in an array in memory. If the user’s name is in this list, display a suitable greeting; otherwise

#include

#include

using namespace std;

#define COUNT 20

int main ( )

{

string list[COUNT];

int i=0;

for(i=0; i

cin >>list[i]; //输入名字列表

string input;

cout << "please input the name you want search:" << endl;

cin >> input;

for(i=0; i

{

if(input == list[i])

{

cout <<"welcome " << list[i] << endl;

return 0;

}

}

return 0 ;

}

10. Write a program to ask a user for their name. The user’s name is then compared with a list of names

held in an array in memory. If the user’s name is in this list, display a suitable greeting; otherwise

#include

#include

using namespace std;

int main ( )

{

int i=0;

string number = ""; //定义一个字符串

cin >> number;

int num=0;

for(i=0; i

{

if(number[i]>'9' || number[i]<'0')

{

cout << "your input is error." << endl;

return 0;

}

else

{

int digit = number[i] - '0';

num = num * 10 + digit; //将字符转化为数值

}

}

cout << num+1 << endl;

return 0 ;

}

11. The following is a list of countries and their capital cities.

Australia Canberra

Belgium Brussels

China Beijing

Denmark Copenhagen

England London

France Paris

Greece Athens

Ireland Dublin

Scotland Edinburgh

Wales Cardiff

Write a program to input a country and display the capital city of that country.

#include

#include

using namespace std;

#define COUNT 10

struct country_city{

string country;

string city;

};

int main ( )

{

struct country_city CC[COUNT]; //定义结构体数组

int i=0;

for(i=0; i

cin >> CC[i].country >> CC[i].city;

string input;

cin >> input;

for(i=0; i

{

if(input == CC[i].country)

{

cout << CC[i].city << endl;

return 0;

}

}

return 0 ;

}

12. Initialise an array of strings with the following quotations:

“There is no reason for any individual to have a computer in their home.”

“Computers are useless. They can only give you answers.”

“To err is human, but to really foul things up requires a computer.”

“The electronic computer is to individual privacy what the machine gun was to the horse cavalry.” Input a word from the keyboard and display all quotations, if any, containing that word.

#include

#include

using namespace std;

#define COUNT 4

int main ( )

{

string str[COUNT];

str[0]="There is no reason for any individual to have a computer in their home.";

str[1]="Computers are useless. They can only give you answers.";

str[2]="To err is human, but to really foul things up requires a computer.";

str[3]="The electronic computer is to individual privacy what the machine gun was to the

horse cavalry.";

string word;

cin >> word;

for(int i=0; i

{

if(str[i].find(word) >= 0 && str[i].find(word)

cout << str[i] << endl;

}

return 0 ;

}

13. Input two strings from the keyboard and check if they are anagrams of each other.

Hint: Take each character of the first string and check that it exists in the second string. If it doesn’t

then the strings are not anagrams of each other. If the character does exist in the second string, remove

it and continue to the next character of the first string. When all the characters of the first string have

been processed, the second string should be empty. If it is, then the two strings are anagrams of each

other. Use C++ strings.

#include

#include

using namespace std;

int main ( )

{

string str1,str2;

cin >> str1 >> str2;

if(str1.length() != str2.length())

{

cout << "they are not anagrams of each other!" << endl;

return 0;

}

for(int i=0; i

{

if(str2.find(str1[i]) >= 0 && str2.find(str1[i]) < str2.length())

str2.erase(str2.find(str1[i]),1);

else

{

cout << "they are not anagrams of each other!" << endl;

return 0;

}

}

cout << "they are anagrams of each other!" << endl;

return 0 ;

}

Report this document

For any questions or suggestions please email
cust-service@docsford.com