C++ 尝试反转C字符串

C++ 尝试反转C字符串,c++,algorithm,reverse,c-strings,function-definition,C++,Algorithm,Reverse,C Strings,Function Definition,除了strlen(),我不能使用任何c函数,也不能使用字符串。不幸的是,我已经在这里呆了很长时间了。我一直在输出奇怪的字符。i、 e问号和本质上奇怪的alt代码就是它的样子 #include <iostream> #include <cstring> using namespace std; int lastIndexOf(const char*, char[]); void reverse(char*); int replace(char*, char, char)

除了
strlen()
,我不能使用任何c函数,也不能使用字符串。不幸的是,我已经在这里呆了很长时间了。我一直在输出奇怪的字符。i、 e问号和本质上奇怪的alt代码就是它的样子

#include <iostream>
#include <cstring>

using namespace std;

int lastIndexOf(const char*, char[]);
void reverse(char*);
int replace(char*, char, char);

int main() {
  int i = 0, count = 0, counter = 0, SIZE = 100;
  char charArray[SIZE];
  cout << "Enter a string of no more than 50 characters: ";
  cin.getline(charArray, SIZE);
  reverse(charArray);
}

void reverse(char s[])
{
  int n = 100;

  for (int i = 0; i < n / 2; i++) {
    swap(s[i], s[n - i - 1]);
    cout << (s[i]);
  }
}
#包括
#包括
使用名称空间std;
int lastIndexOf(常量字符*,字符[]);
无效反向(字符*);
int替换(char*,char,char);
int main(){
int i=0,count=0,counter=0,SIZE=100;
字符字符[大小];

cout该函数使用幻数100

int n = 100;
虽然在main中有一个输入不超过50个字符的提示

cout << "Enter a string of no more than 50 characters: ";
<>注意可变长度数组不是标准C++特性。 你应该写

const size_t SIZE = 100;
char charArray[SIZE];

为什么你认为用户输入了100个字符?
int n=100;
是你的错误,提示:你告诉我们你可以使用
strlen
,但你从来没有在你的代码中使用过它!为什么你认为你需要被允许使用该函数?知道字符串的长度在哪里很重要?(另请参见上述注释).fwiw,这不是最简单的事情。不要害怕,因为你期望事情简单,但你却在挣扎。事情并不简单。只有当你设法让它工作时,事情才“简单”@God_Zilla121为什么你可以使用
std::swap()
但不能?在任何情况下,
int n=100;
应该是
int n=strlen(s);
相反。或者更好的做法是,在
reverse()
函数中添加一个
size
参数,然后您可以使用
cin.gcount()
作为大小,根本不需要
strlen()
const size_t SIZE = 100;
char charArray[SIZE];