C++ 使用c语言处理文件
如何从字符串中删除每个不是字母的字符 从字符串C++ 使用c语言处理文件,c++,file,C++,File,如何从字符串中删除每个不是字母的字符 从字符串a、f、4、h、b, 我需要一个输出afhbi。 请注意,我不想逗号和其他类似的迹象。 这是我到目前为止的代码,它不起作用,有什么提示吗 while((fgets(str,30,fpointer))!=NULL) { //i get a string for(i=0;i<strlen(str);i++)//going thru the string if(isalpha(str[i])){strcat(Need,str[i])
a、f、4、h、b
,
我需要一个输出afhbi
。
请注意,我不想逗号和其他类似的迹象。
这是我到目前为止的代码,它不起作用,有什么提示吗
while((fgets(str,30,fpointer))!=NULL)
{ //i get a string
for(i=0;i<strlen(str);i++)//going thru the string
if(isalpha(str[i])){strcat(Need,str[i]);}
//if the char is alpha put it in a new string called Need
}
while((fgets(str,30,fpointer))!=NULL)
{//我得到一个字符串
对于(i=0;i您不想使用strcat
将字符添加到数组中。这是为了将一个字符串附加到另一个字符串上。只需在数组中插入字符即可
int j = 0; // Index of the new string
for(i = 0; i < strlen(str); i++) { //going thru the string
if(isalpha(str[i])) {
Need[j++] = str[i];
}
}
Need[j] = 0; // Make sure you terminate the new string
int j=0;//新字符串的索引
对于(i=0;i
您也可以使用memmove执行类似操作。首先复制需要的字符串
Need = strdup(str);
p = Need;
q = str;
while (*q) {
if (!isalpha(*q)) {
len = strlen(p);
memmove(p, p + 1, len); // this will move the NULL terminator too
} else {
p++;
}
q++;
}
现在,所有丑陋的非角色都需要清理