运行以下程序#include iostream#include cstringusing namespace std;int main char strSource = AABADCDABDEA; int len = strlenstrSource; forint i = 0; i len; i++ ifstrSourcei == A
该程序会编译错误,因为将一个字符串常量赋值给一个 char* 指针是不允许的。应该使用 const char* 或者 char[] 来声明字符串。
正确的程序应该是:
#include
using namespace std;
int main() { const char* strSource = "AABADCDABDEA"; int len = strlen(strSource); char strDest[len+1]; strcpy(strDest, strSource); for(int i = 0; i < len; i++) { if(strDest[i] == 'A') { strDest[i] = 'C'; } } cout << strDest << endl; return 0; }
原文地址: https://www.cveoy.top/t/topic/ffgT 著作权归作者所有。请勿转载和采集!