gpt4 book ai didi

c - 字符串计数器的词频有时会出错

转载 作者:行者123 更新时间:2023-11-30 15:05:46 24 4
gpt4 key购买 nike

我希望你能帮助我编写这段代码。代码的工作原理如下

  • 用户输入一个字符串,例如“hey john, how are you john?
  • 程序会删除诸如“'?'之类的符号、'、''!' “等等
  • 程序在删除符号后写入一个字符串:“hey john how are you john?”

  • 代码输出每个单词的频率:

嘿:1约翰:2如何:1是:1你:1

但是我的代码有时会出错。例如,当我输入“再见再见你好你好你好”

输出是:再见:3你好:1

我的代码对 john 示例进行了正确的处理,但对 bye bye... 示例进行了错误处理。

我该如何更改我的代码?谢谢

#include <stdio.h>
#include <string.h>


char words[80][80];

void clear_string(char *text);
int extract_and_count(char *source, int *count);
void clearArray(char array[]);
int indexInWords(char string[]);
void print(int countOfWords, int count[]);
int equals(char *string1, char *string2);


int main() {
char string[80];
int count[80];

printf("please enter your text: ");

scanf("%[^\n]s", string);

clear_string(string);

printf("%s\n", string);
int countOfWords = extract_and_count(string, count);

print(countOfWords, count);

return 0;
}

void clear_string(char *text){

int i = 0;

for(;i < strlen(text);++i){
if( text[i] == '.' || text[i] == ',' || text[i] == '!' || text[i] == '?'){

int k = i + 1;
for(; k < strlen(text);++k){
text[k-1] = text[k];
}

k = strlen(text) - 1;
text[k] = ' ';
}
}
}

int extract_and_count(char *source, int *count){

int wordCounter = 0;
char string[80];
int i = 0, k = 0;
clearArray(string);

for(; i < strlen(source);++i, ++k){

if(source[i] != ' '){
string[k] = source[i];
}else{
if(string[0] == '\0'){
break;
}
int index = indexInWords(string);

if(index == -1){

strcpy(words[wordCounter], string);
count[wordCounter] = 1;
wordCounter++;
}else{
count[index] += 1;
}

clearArray(string);
k = -1;


}

}

return wordCounter;
}

void clearArray(char array[]){
memset(array,0,strlen(array));
//array[0] = '\0';
}

int indexInWords(char string[]){

int i = 0;
for(;i < 80;++i){
if(equals(words[i], string) == 0){
return i;
}
}

return -1;
}

void print(int countOfWords, int count[]){


for(int i = 0;i < countOfWords; ++i){
printf("%s : %d\n",words[i], count[i]);

}






}

int equals(char string1[], char string2[]){



return strcmp(string1, string2);
}

最佳答案

我发现的最重要的问题是在 extract_and_count() 中——它不计算最后一个单词,因为它只计算后面跟着空格的单词。 bandaid 的作用是在循环后检查 string 中是否有任何内容,如果有,则处理它。以下是我对该修复和一般风格的修改:

#include <stdio.h>
#include <string.h>
#include <stdbool.h>

void clear_string(char *text);
int extract_and_count(char *source, int count[]);
void clearArray(char array[]);
int indexInWords(char string[]);
void print(int countOfWords, int count[]);
bool equals(char *string1, char *string2);

#define BUFFER_SIZE (512)
#define MAX_WORD_COUNT (80)
#define MAX_WORD_SIZE (64)

char words[MAX_WORD_COUNT][MAX_WORD_SIZE];

int main() {
char string[BUFFER_SIZE];
int count[MAX_WORD_COUNT];

printf("Please enter your text: ");

while (fgets(string, BUFFER_SIZE, stdin) == NULL) {
printf("Please (re)enter your text: ");
}

clear_string(string);

int countOfWords = extract_and_count(string, count);

print(countOfWords, count);

return 0;
}

void clear_string(char *text) {

for (int i = 0; i < strlen(text); i++) {
if (text[i] == '.' || text[i] == ',' || text[i] == '!' || text[i] == '?' || text[i] == '\n') {

int length = strlen(text);

for (int k = i + 1; k < length; k++) {
text[k - 1] = text[k];
}

text[length - 1] = '\0';

i--;
}
}
}

int extract_and_count(char *source, int count[]) {

int wordCounter = 0;
char string[MAX_WORD_SIZE] = {'\0'};

for (int i = 0, k = 0; i < strlen(source); i++, k++) {

if (source[i] != ' ') {
string[k] = source[i];
} else {
if (string[0] == '\0') {
break;
}
int index = indexInWords(string);

if (index == -1) {
strcpy(words[wordCounter], string);
count[wordCounter] = 1;
wordCounter++;
} else {
count[index] += 1;
}

clearArray(string);
k = -1;
}
}

if (string[0] != '\0') {
int index = indexInWords(string);

if (index == -1) {
strcpy(words[wordCounter], string);
count[wordCounter] = 1;
wordCounter++;
} else {
count[index] += 1;
}
}

return wordCounter;
}

void clearArray(char array[]) {
memset(array, 0, strlen(array));
}

int indexInWords(char string[]) {

for (int i = 0; i < MAX_WORD_COUNT; i++) {
if (equals(words[i], string)) {
return i;
}
}

return -1;
}

void print(int countOfWords, int count[]) {

for (int i = 0; i < countOfWords; i++) {
printf("%s : %d\n", words[i], count[i]);
}
}

bool equals(char string1[], char string2[]) {
return strcmp(string1, string2) == 0;
}

我看到的下一个最重要的问题是您没有跟踪使用了 words[][] 中的条目数量,因此 indexInWords() 可以轻松地与未初始化的内存进行比较。

关于c - 字符串计数器的词频有时会出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39648100/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com