两种方法将一段英文文字拆分成单词依次输出,但是一个成功,一个失败
#define _STDC_WANT_LIB_EXT1_1#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#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 <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#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;
}
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#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;
}
这是第二种方法失败了。