[英]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.