本帖最后由 bin554385863 于 2019-10-27 23:28 编辑 /*
这段代码的作用是去除字符串中不是字母的字符
*/
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <ctype.h>
char *func(char *cstr)
{
int count = 0, m = 32;
char *ptr = (char *)malloc(sizeof(char) * m); //申请空间
for (char *i = cstr; *i != '\0'; i++)
{
if (isalpha(*i))//判断是否为字母
{
ptr[count] = *i;
ptr[count + 1] = '\0';
count++; //计算大小
if (count > (m / 2)) //空间不足时.动态申请空间
{
m += 16;
char *tmp = (char *)malloc(sizeof(char) * m);
tmp[m] = {0};
strcpy(tmp, ptr);
free(ptr);
ptr = tmp;
}
}
}
return ptr;
}
int main(int argc, char const *argv[])
{
char str[] = "A D D C";
printf("%s", func(str));
return 0;
}
---------------------------------------------------------------------------------------
Microsoft Windows [版本 10.0.16299.1087]
(c) 2017 Microsoft Corporation。保留所有权利。
E:\Users\86184\Documents\Code>c:\Users\86184\.vscode\extensions\ms-vscode.cpptools-0.26.0\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-kexbbu1a.bor --stdout=Microsoft-MIEngine-Out-mm3agyom.2cm --stderr=Microsoft-MIEngine-Error-mo4hduvp.fz1 --pid=Microsoft-MIEngine-Pid-4njrtxpr.bc1 "--dbgExe=E:\My Program\MinGW\bin\gdb.exe" --interpreter=mi
ADDC
E:\Users\86184\Documents\Code> |