此程序应将 n't 后跟字母更改为“not”。喜欢不要->不要。但它显示了一些非常奇怪的输出。例如,如果我只是跳过打印字符串长度输出是垃圾。我也不能说空间如何在输出中变为 s2ace。如果我不知道更好,我会说我在代码中有一个幽灵。
#include<stdio.h>
#include<string.h>
#include<ctype.h>
int func(char **str, int cur_len, int max_len)
{
if (cur_len <= 3)
return(cur_len);
else if(max_len < cur_len)
return(-1);
int i,j;
char new[max_len];
new[0]='\0';
char temp[2];
for (i = 0; i < cur_len; i++)
{
if(i > 0 && i < cur_len - 2 && isalpha((*str)[i-1]) && (*str)[i] == 'n' && (*str)[i+1] == '\'' && (*str)[i+2] == 't')
{
strcat(new," not");
i=i+2;
}
else
{
temp[0]=(*str)[i];
temp[1]='\0';
strcat(new, temp);
}
}
j = strlen(new);
if (j > max_len)
return(-1);
*str = new;
return(j);
}
int main()
{
char *x = "Please don't do anything stupid. all the n't followed by an alphabate should be changed to not followed by a space. e.g. doesn't.";
int len, max, result;
len = strlen(x);
printf("String size = %d\n",len);//**if i comment this line all output goes down the drain.**
max = 200;
result = func (&x, len, max);
if(result == -1)
{
printf("No change possible\n");
return(-1);
}
printf("No of changes = %d\n",strlen(x)-len);
printf("New String is :: %s\n",x);//**how does space change into s2ace??**
return(0);
}
输出是
129No of changes = 2 New String is :: 请不要做任何愚蠢的事情。所有不跟随字母的 n 都应更改为不跟随 s2ace。例如没有。