我是靠谱客的博主 调皮曲奇,最近开发中收集的这篇文章主要介绍C++ Primer String 相关习题,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

#include <string>
#include <iostream>
#include <vector>
// Practise 3.5
// Author : Jun
// Data: Sept.15
// 从标准输入每次读取一行文本:
// getline(cin,line)
// 每次读取一个单词
// cin >> word


// Practice 3.7
// 编一个程序读入两个string对象,测试它们是否相等。若不相等,则指出两个中哪个较大。接着改写程序测试它们的长度是否相等,若不相同指出哪个较长。
void main37()
{
std::string sa,sb;
// to test what is in the empty string;--> NULL(n)
//std::cout << sa << std::endl;
std::cin >> sa >> sb;
//Question 1:
if(sa == sb)
std::cout << "They are the same : " << sa << std::endl;
else if (sa > sb)
std::cout << "String " << sa <<" is bigger than " << sb << std::endl;
else
std::cout << "String " << sb <<" is bigger than " << sa << std::endl;
//Question 2:
if (sa.size() == sb.size())
std::cout << "They are the same : " << sa.size() << std::endl;
else if (sa.size() > sb.size())
std::cout << "String " << sa <<" is longer than " << sb << std::endl;
else
std::cout << "String " << sb <<" is longer than " << sa << std::endl;
}


// Practice 3.8
// Write a program to read string s from the standard input,concatenating what is read into one large string .
// Print the concatenated string .
// Next, change the program to separate adjacent input string s by a space.
int main38()
{
std::string desS;
std::string newS;


while(std::cin >> newS) // Pay Attention! Here you must use Ctrl+Z  to inform computer your input ends(Also after the Enter key)
desS = desS+ newS +" "  ;


std::cout << desS << std::endl;


return 1;
}


// Practice 3.9
// Empty String
int main39()
{
std::string a;
std::cout << a[0] << std::endl;// error : subscript out of range


return 1;
}


// Practice 3.10
// Write a program to strip the punctuation from a string . The input to the program should be a string of characters including
// punctuation; the output should be a string in which the punctuation is removed.


void main310()
{
std::string str;
getline(std::cin,str);
std::cout << "The original string is :n" << str << std::endl;
// use the Containers idea
std::string::iterator itS = str.begin();
while(itS != str.end())
{
if( ispunct(*itS) ) // the char is a punctuation
itS = str.erase(itS); // return the next iterator behind the erased one.
else  itS++;
}
std::cout << "The changed string is :n" << str << std::endl;
}


//  Exercise
//  9.34:
//Use iterators to change the characters in a string to uppercase.
void main934()
{
std::string str = "You are so Bright!n";
std::cout << str;
std::string::iterator strI = str.begin();
while( strI!= str.end() )
{
*strI = toupper(*strI);
strI++;
}
std::cout << str;
}


// Exercise
// 9.35:
//Use iterators to find and to erase each capital letter from a string .
void main935()
{
std::string str ("You Are So Bright!n");
std::cout << str;
std::string::iterator strI = str.begin();
while( strI!= str.end() )
{
if(isupper(*strI))
strI = str.erase(strI);
else
strI++;
}
std::cout << str;
}


// Exercise
// 9.36: Pay Attention 1,use push_back to initialize the vector<char>;2,use iterator to initialize the string
//Write a program that initializes a string from a vector<char> .
void main936()
{
std::vector<char> CV ;

char Nc;
while(std::cin >> Nc)
{
CV.push_back(Nc);
}


std::string str(CV.begin(),CV.end());
std::cout << str;



}


// Exercise
// 9.37:
//  Given that you want to read a character at a time into a string , and you know that the data you need to read is at
// least 100 characters long, how might you improve the performance of your program?
//  Answer: Since most of running time is the time for memory allocation, we can prepare more than 100 char space with reserve(120)


//Exercise
// 9.38:
//Write a program that, given the string
// "ab2c3d7R4E6"
// "A" is 65, "a" is 97
// finds each numeric character and then each alphabetic character. Write two versions of the program. 
//  The first should use find_first_of , and the second find_first_not_of .
void main938()
{
std::string numeric("0123456789");
std::string alphabetic("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
std::string str( "ab2c3d7R4E6" );
std::string::size_type pos(0);
while((pos = str.find_first_of(numeric,pos)) != std::string::npos )
{
std::cout << "the " << pos +1<< "th Number is "<< str[pos] <<"n";
pos++;
}


pos = 0;
while((pos = str.find_first_of(alphabetic,pos)) != std::string::npos )
{
std::cout << "the " << pos+1 << "th Letter is "<< str[pos] <<"n";
pos++;
}
pos = 0;
while((pos = str.find_first_not_of(numeric,pos)) != std::string::npos )
{
std::cout << "the " << pos +1<< "th non-Number is "<< str[pos] <<"n";
pos++;
}


}


// Exercise
// 9.39:
//Write a program that, given the string s
// string line1 = "We were her pride of 10 she named us:";
//string line2 = "Benjamin, Phoenix, the Prodigal"
// string line3 = "and perspicacious pacific Suzanne";
//string sentence = line1 + ' ' + line2 + ' ' + line3;
//counts the number of words in sentence and identifies the largest and
// smallest words. If several words have the largest or smallest length,
// report all of them.
It is going soon...


//Exercise
// 9.40:
//Write a program that accepts the following two string s:
//string q1("When lilacs last in the dooryard bloom'd");
//string q2("The child is father of the man");
//Using the assign and append operations, create the string
// string sentence("The child is in the dooryard");
void main940()
{
std::string q1("When lilacs last in the dooryard bloom'd");
std::string q2("The child is father of the man");
std::string::size_type pq1= q1.find("in");
std::string sent;
sent.assign(q2.begin(),q2.begin()+13);//"The child is " total 13 letters
sent.append(q1,pq1,16);//"in the dooryard" total 16 letters
std::cout <<sent << std::endl;
}


//Exercise
// 9.41:
//Write a program that, given the string s
// string generic1("Dear Ms Daisy:");
//string generic2("MrsMsMissPeople");
//implements the function
// string greet(string form, string lastname, string title,
// string::size_type pos, int length);
//using the replace operations, where lastname replaces Daisy and pos
// indexes into generic2 of length characters replacing Ms . For example, the
// following
// string lastName("AnnaP");
//string salute = greet(generic1, lastName, generic2, 5, 4);
//returns the string
// Dear Miss AnnaP:
std::string greet(std::string form, std::string lastname, std::string title, std::string::size_type pos, int length)
{
//Change lastname ,change title
// find the position of title and lastname and then replace them
//std::string separators(":tnvrf");
std::string separators(": ");
std::string::size_type pt(0),pn(0),nameend(0);
pt = form.find_first_of(separators) + 1;
pn = form.find_first_of(separators,pt) + 1;
nameend = form.find_first_of(separators,pn);
// replace the lastname
form.replace(pn,nameend-pn,lastname);
// change title
form.replace(pt,pn-pt-1,title.substr(pos,length));


return form;
}
 
void main()
{
std::string generic1("Dear Ms Daisy:");
std::string generic2("MrsMsMissPeople");
std::string lastName("AnnaP");
std::string salute = greet(generic1, lastName, generic2, 5, 4);


std::cout << salute <<std::endl;
}

最后

以上就是调皮曲奇为你收集整理的C++ Primer String 相关习题的全部内容,希望文章能够帮你解决C++ Primer String 相关习题所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(48)

评论列表共有 0 条评论

立即
投稿
返回
顶部