字符串常量不可更改
#include <iostream> #include <cstdlib> #include <cstring> using namespace std; void deleteStr(char * str) { int nSpaceNum = 0; char *p = str; char *q = str; char *s = str; while(*s != ‘\0‘) { if(*s == ‘ ‘) { nSpaceNum++; q++; } else { *p = *q; *q = ‘ ‘; p++; q++; } s++; } cout<<"nSpaceNum=="<<nSpaceNum<<endl; cout<<"str=="<<str<<endl; } int main() { char* str = " hell o w orl d"; char *dest = (char*)malloc(sizeof(char)*strlen(str)); char *d = dest; while(*str != ‘\0‘) *d++ = *str++; deleteStr(dest); return 0; }
时间: 2024-10-07 13:48:34