<分区>
这是我的程序,用于匹配子字符串。我想更改程序中输入和输出的工作方式。
问题定义:
- 提示用户输入多少个字符串(N)
- N个字符串将分行输入
- 对于每个字符串,
- 如果它以“hackerrank”开头,则打印 1
- 如果以“hackerrank”结尾则打印 2
- 如果以“hackerrank”开头和结尾则打印 0
- 如果以上都不是则打印-1
示例:
输入:
4
i love hackerrank
hackerrank is an awesome place for programmers
hackerrank
i think hackerrank is a great place to hangout
Output:
2
1
0
-1
这是我的实际代码。
int main()
{
vector<string> token_store;
string s,token;
getline(cin,s);
std::istringstream iss(s);
while(iss>>token)
token_store.push_back(token); //splitting strings into tokens
int len=token_store.size(); //storing size of vector
if(token_store[0]=="hack" && token_store[len-1]=="hack")
cout<<"first and last same"; //if first and last word of input string is hack
else if(token_store[0]=="hack")
cout<<"first matches"; //if first word of input string is hack
else if(token_store[len-1]=="hack")
cout<<"last matches"; //if last word of input string is hack
}