C++程序获取字符串的长度,执行连接和交换字符
假设我们有两个字符串s和t,我们必须在三行中找到输出,第一行包含用空格分隔的s和t的长度,第二行保存s和t的连接,第三行包含s和t由空格分隔,但它们的第一个字符被交换。
因此,如果输入类似于s="hello",t="programmer",那么输出将是
5 10 helloprogrammer pello hrogrammer
示例
让我们看看以下实现以获得更好的理解-
#includeusing namespace std; int main(){ string s = "hello", t = "programmer"; cout << s.length() << " " << t.length() << endl; cout << s + t << endl; char temp = s[0]; s[0] = t[0]; t[0] = temp; cout << s << " " << t << endl; }
输入
"hello", "programmer"输出结果
5 10 helloprogrammer pello hrogrammer
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短