CPP58. 编写函数实现字符串翻转(引用方式)
描述
编写一个函数,实现字符串的翻转,要求采用引用的方式实现。输入描述
输入一个字符串输出描述
输出翻转后的字符串示例1
输入:
abc
输出:
cba
C++ 解法, 执行用时: 2ms, 内存消耗: 308KB, 提交时间: 2022-03-13
#include<bits/stdc++.h> using namespace std; // write your code here...... void strback(string &s) { int len = s.length(); for (int i = 0; i < len/2; i++) { char temp = s[len - i - 1]; s[len - i - 1] = s[i]; s[i] = temp; } } int main(){ string s; getline(cin,s); // write your code here...... strback(s); cout<<s; return 0; }
C++ 解法, 执行用时: 2ms, 内存消耗: 456KB, 提交时间: 2022-03-13
#include<bits/stdc++.h> using namespace std; void flip(string &s){ int b = s.length(); for(int i = 0;i<b/2;i++){ char a = s[i]; s[i] = s[b - i -1]; s[b - i - 1] = a; } } int main(){ string s; getline(cin,s); // write your code here...... flip(s); cout<<s; return 0; }
C++ 解法, 执行用时: 3ms, 内存消耗: 392KB, 提交时间: 2022-08-05
#include<bits/stdc++.h> using namespace std; // write your code here...... void turnStr(string& s) { for(int i = 0; i < s.size() / 2; i++) { size_t size = s.size(); char temp = s[i]; s[i] = s[size - i - 1]; s[size - i - 1] = temp; } } int main(){ string s; getline(cin,s); // write your code here...... turnStr(s); cout<<s; return 0; }
C++ 解法, 执行用时: 3ms, 内存消耗: 392KB, 提交时间: 2022-06-03
#include<bits/stdc++.h> using namespace std; // write your code here...... void swap(string& s){ char temp; for (int i=0,n=s.length();i<(n/2);i++){ temp=s[i]; s[i]=s[n-i-1]; s[n-i-1]=temp; } return; } int main(){ string s; getline(cin,s); // write your code here...... swap(s); cout<<s; return 0; }
C++ 解法, 执行用时: 3ms, 内存消耗: 392KB, 提交时间: 2022-05-03
#include<bits/stdc++.h> using namespace std; #include <string> // write your code here...... void myreverse(string &a) { for(int i=0;i<a.length()/2;i++) { char t=a[a.length()-i-1]; a[a.length()-i-1]=a[i]; a[i]=t; } } int main(){ string s; getline(cin,s); // write your code here...... myreverse(s); cout<<s; return 0; }