[英]String manipulation C++
我需要做一些功能,但我不知道如何。
示例:s1=first,s2=second; s1+s2=firstsecond,pos=3,7,10,輸出=ren
這是我的一個位置的代碼,但我不知道如何制作多個位置,主要問題是如何限制位置的輸入:
s=s1+s2;
cin>>pos;
cout<<s[pos-1];
示例:s=firstsecondthird,元音=i,str=EXA,輸出=fEXArstsecondthEXArd
這就是我所知道的,我不知道如何用 string(str) 替換元音
cin>>vowel;
if(check is defined character vowel)
{
cin>>str;
.
.
.
}
謝謝
抓住! :)
#include <iostream>
#include <string>
#include <cstring>
int main()
{
std::cout << "Enter first string: ";
std::string s1;
std::cin >> s1;
std::cout << "Enter second string: ";
std::string s2;
std::cin >> s2;
s1 += s2;
std::cout << "The joined string is " << s1 << std::endl;
std::cout << "Enter several positions in the joined string (0-stop): ";
std::string s3;
std::string::size_type pos;
while ( std::cin >> pos && pos != 0 )
{
if ( --pos < s1.size() ) s3 += s1[pos];
}
std::cout << "You have selected the following letters " << s3 << std::endl;
const char *vowels = "aeiou";
char c;
do
{
c = '\0';
std::cout << "Enter a vowel: ";
} while ( std::cin >> c && !std::strchr( vowels, c ) );
if ( c != '\0' )
{
for ( auto pos = s1.find( c, 0 );
pos != std::string::npos;
pos = s1.find( c, pos ) )
{
const char *t = "EXA";
const size_t n = 3;
s1.replace( pos, 1, t );
pos += n;
}
std::cout << "Now the joined string looks like " << s1 << std::endl;
}
return 0;
}
如果進入
first
second
3 7 10 0
i
那么程序輸出將是
Enter first string: first
Enter second string: second
The joined string is firstsecond
Enter several positions in the joined string (0-stop): 3 7 10 0
You have selected the following letters ren
Enter a vowel: i
Now the joined string looks like fEXArstsecond
您可以將其用作出色程序的模板。:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.