This Program should change n't followed by an alphabet to " not". like don't -> do not. But it's showing some really weird outputs. e.g if i just skip printing the string length output is garbage. also how space is changing to s2ace in output i can't say. If i didn't know better i'd say i have a ghost in the code.
#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);
}
Output is
129No of changes = 2 New String is :: Please do not do anything stupid. all the n't followed by an alphabate should be changed to not followed by a s2ace. e.g. does not.