仅删除字母字符的句子
Stripping a sentence for only it's alpha characters
我正在尝试解决一个代码,使用下面的代码将一个句子剥离成字母字符,但代码总是给我一个运行时错误(注释部分是我为弄清楚而采取的步骤解决方案)。
[例如:Test'sentence 应该打印 Testsentence]
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define BUFFER_LEN 1000
#define BUFFER_INCR 15
int main(void)
{
int buffer_length = BUFFER_LEN;
char *pString = malloc(BUFFER_LEN);/* initial array */
char *pTemp_start = pString;
long long int String_len = 0;
char *pTemp = NULL;
int copy = 0;
int count = 0;/*delete this after check*/
while((*pString++ = getchar()) != '\n')
{
String_len = pString - pTemp_start;
printf("\nThe character you inputted is: %c", *(pString+count++));
//getchar();
if(String_len == (buffer_length - 1))/*reserve one for newline*/
{
buffer_length += BUFFER_INCR;
pTemp = realloc(pString, buffer_length);/*reallocate space for
15 more chars.*/
pTemp_start = pTemp - String_len;
pString = pTemp;
free(pTemp);
pTemp = NULL;
if(!pString)
{
printf("The space couldn't be allocated");
return 1;
}
}
}
/*checks that can be done for addresses*/
//printf("\nThe length of the string is: %lld", pString - pTemp_start);
*(--pString) = '[=10=]';
//printf("\nThe charcter at the end is: %d", *(pString + String_len - 1));
//printf("\nThe character at the mid is: %d", *(pString + 2));
printf("The input string is: %c", *pString);
/*code to remove spaces*/
for(int i = 0; i < (String_len + 1); i++)
{
if((isalnum(pString[i])))
{
*(pString + copy++) = *(pString +i);
}
}
*(pString + copy) = '[=10=]';/*append the string's lost null character*/
printf("\nThe stripped string is: \n%s", pString);
return 0;
}
代码根本不打印任何输入的内容。
realloc(pString,...)
不会 添加 分配的块,它 替换 正在重新分配的块(在这种情况下, pString
).因此 pString
不是(必然)调用后的有效指针。更糟糕的是,你 free(pTemp)
,所以你不再分配 任何东西。
所以你的代码在这一行之间有冲突
while((*pString++ = getchar()) != '\n')
和类似下面的行。
pTemp = realloc(pString, buffer_length);
我引用的第一行是递增 pString 在分配的内存中的位置,但第二行就像 pString 仍指向它的开头一样。 realloc()
将不起作用,除非 pString 指向已分配内存的开头。然后,您不会检查 realloc()
调用的结果,将新的内存块分配给 pString,然后释放新分配的内存。所以你一定会有意想不到的结果。
您还必须记住 stdin 是缓冲的,因此您的代码将等到它有整行要读取后再执行任何操作。并且 stdout 也被缓冲,因此只会输出以 \n
结尾的行。所以你可能想要以下...
printf("The character you inputted is: %c\n", *pString);
...或类似的东西,请记住您使用 pString 的方式的问题。
我正在尝试解决一个代码,使用下面的代码将一个句子剥离成字母字符,但代码总是给我一个运行时错误(注释部分是我为弄清楚而采取的步骤解决方案)。
[例如:Test'sentence 应该打印 Testsentence]
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define BUFFER_LEN 1000
#define BUFFER_INCR 15
int main(void)
{
int buffer_length = BUFFER_LEN;
char *pString = malloc(BUFFER_LEN);/* initial array */
char *pTemp_start = pString;
long long int String_len = 0;
char *pTemp = NULL;
int copy = 0;
int count = 0;/*delete this after check*/
while((*pString++ = getchar()) != '\n')
{
String_len = pString - pTemp_start;
printf("\nThe character you inputted is: %c", *(pString+count++));
//getchar();
if(String_len == (buffer_length - 1))/*reserve one for newline*/
{
buffer_length += BUFFER_INCR;
pTemp = realloc(pString, buffer_length);/*reallocate space for
15 more chars.*/
pTemp_start = pTemp - String_len;
pString = pTemp;
free(pTemp);
pTemp = NULL;
if(!pString)
{
printf("The space couldn't be allocated");
return 1;
}
}
}
/*checks that can be done for addresses*/
//printf("\nThe length of the string is: %lld", pString - pTemp_start);
*(--pString) = '[=10=]';
//printf("\nThe charcter at the end is: %d", *(pString + String_len - 1));
//printf("\nThe character at the mid is: %d", *(pString + 2));
printf("The input string is: %c", *pString);
/*code to remove spaces*/
for(int i = 0; i < (String_len + 1); i++)
{
if((isalnum(pString[i])))
{
*(pString + copy++) = *(pString +i);
}
}
*(pString + copy) = '[=10=]';/*append the string's lost null character*/
printf("\nThe stripped string is: \n%s", pString);
return 0;
}
代码根本不打印任何输入的内容。
realloc(pString,...)
不会 添加 分配的块,它 替换 正在重新分配的块(在这种情况下, pString
).因此 pString
不是(必然)调用后的有效指针。更糟糕的是,你 free(pTemp)
,所以你不再分配 任何东西。
所以你的代码在这一行之间有冲突
while((*pString++ = getchar()) != '\n')
和类似下面的行。
pTemp = realloc(pString, buffer_length);
我引用的第一行是递增 pString 在分配的内存中的位置,但第二行就像 pString 仍指向它的开头一样。 realloc()
将不起作用,除非 pString 指向已分配内存的开头。然后,您不会检查 realloc()
调用的结果,将新的内存块分配给 pString,然后释放新分配的内存。所以你一定会有意想不到的结果。
您还必须记住 stdin 是缓冲的,因此您的代码将等到它有整行要读取后再执行任何操作。并且 stdout 也被缓冲,因此只会输出以 \n
结尾的行。所以你可能想要以下...
printf("The character you inputted is: %c\n", *pString);
...或类似的东西,请记住您使用 pString 的方式的问题。