C语言怎么把单词拆成字母,两种方法将一段英文文字拆分成单词依次输出,但是一个成功,一个失败...

两种方法将一段英文文字拆分成单词依次输出,但是一个成功,一个失败

#define _STDC_WANT_LIB_EXT1_1

#include

#include

#include

#include

#include

#define max_word 20

size_t wordscount(char *);

char** wordfind(char*, char** const);

main(void)

{

size_t str_size = 1000;

char buf_len[100];

char *str_len = malloc(str_size);

*str_len = '\0';

char* ptemp = NULL;

while (true)

{

gets_s(buf_len, sizeof(buf_len));

if (buf_len[0] == '\0')

break;

if (strlen(buf_len) + strlen(str_len) + 1 > str_size)

{

str_size = str_size + 100;

ptemp = realloc(str_len, str_size);

str_len = ptemp;

ptemp = NULL;

}

if (strcat_s(str_len, str_size, buf_len))

{

printf("Somethings wrong!The string concatenation failed!\n");

return 1;

}

}

printf("the words number is %zd\n", wordscount(str_len));

char** words = malloc(wordscount(str_len)*sizeof(char*));

for (size_t i = 0; i < wordscount(str_len); i++)

{

*(words+i) = (char*)malloc(max_word);

}

char** pstr_len=wordfind(str_len, words);

for (size_t i = 0; i < wordscount(str_len); i++)

{

printf("%s\n", pstr_len[i]);

}

return 0;

}

size_t wordscount(char* pstr)

{

size_t i = 0;

size_t count = 0;

while (pstr[i] != '\0')

{

if (!isalpha(pstr[i]))

{

if (pstr[i] == ',')

{

i++;

continue;

}

else

{

count++;

}

}

i++;

}

return count;

}

char** wordfind(char* pstr, char** const words)

{

size_t i = 0;

size_t j = 0;

int count = 0;

while (pstr[i] != '\0')

{

if (pstr[i] == ',')

{

i++;

continue;

}

if (isalpha(pstr[i]))

{

words[count][j] = pstr[i];

i++;

j++;

}

else

{

words[count][j] = '\0';

count++;

j = 0;

i++;

}

}

return words;

}

这是第一个方法,成功了。

#define _STDC_WANT_LIB_EXT1_1

#include

#include

#include

#include

#include

#define max_word 20

size_t wordscount(char *);

char** wordfind(char*, char** const);

main(void)

{

size_t str_size = 1000;

char buf_len[100];

char *str_len = malloc(str_size);

*str_len = '\0';

char* ptemp = NULL;

while (true)

{

gets_s(buf_len, sizeof(buf_len));

if (buf_len[0] == '\0')

break;

if (strlen(buf_len) + strlen(str_len) + 1 > str_size)

{

str_size = str_size + 100;

ptemp = realloc(str_len, str_size);

str_len = ptemp;

ptemp = NULL;

}

if (strcat_s(str_len, str_size, buf_len))

{

printf("Somethings wrong!The string concatenation failed!\n");

return 1;

}

}

printf("the words number is %zd\n", wordscount(str_len));

char** words = malloc(wordscount(str_len)*sizeof(char*));

for (size_t i = 0; i < wordscount(str_len); i++)

{

*(words+i) = (char*)malloc(max_word);

}

char** pstr_len=wordfind(str_len, words);

for (size_t i = 0; i < wordscount(str_len); i++)

{

printf("%s\n", pstr_len[i]);

}

return 0;

}

size_t wordscount(char* pstr)

{

size_t i = 0;

size_t count = 0;

while (pstr[i] != '\0')

{

if (!isalpha(pstr[i]))

{

if (pstr[i] == ',')

{

i++;

continue;

}

else

{

count++;

}

}

i++;

}

return count;

}

char** wordfind(char* pstr, char** const words)

{

char delimiters[] = " \n,.;:?";

char* strword = NULL;

char* pword = strtok_s(pstr, delimiters, &strword);

size_t i = 0;

while (pword)

{

strcpy_s(words[i],strlen(pword)+1,pword);

i++;

pword = strtok_s(NULL, delimiters, &strword);

}

return words;

}

这是第二种方法失败了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值