gpt4 book ai didi

c - 如何编写一个程序,让用户输入尽可能多的字符串并统计出现次数?

转载 作者:行者123 更新时间:2023-11-30 20:29:20 25 4
gpt4 key购买 nike

我需要编写一个程序,让用户输入一个字符串,然后程序询问用户是否继续输入更多字符串。之后,程序应该计算用户输入的所有字符串中单词的所有出现次数。这段代码只计算最后一个字符串的出现次数,我应该如何更改它?

#include <stdio.h>
#include <string.h>
#define MAXSTRLEN 100

int main(void)
{
int count = 0, c = 0, i, j = 0, k, space = 0;
char p[50][100], str1[20], ptr1[50][100];
char* ptr;
char str[MAXSTRLEN], Y_or_N;
printf("Write a sentence.\n");
while (1)
{
gets(str);
printf("Continue or not? (y or n): ");
scanf("%c", &Y_or_N);
getchar();
if (Y_or_N == 'n')
break;
}
for (i = 0; str[i] != '\0'; i++)
{
if ('A' <= str[i] && str[i] <= 'Z')
str[i] = str[i] + 32;
}
for (i = 0; i < strlen(str); i++)
{
if ((str[i] == ' ') || (str[i] == ',' && str[i + 1] == ' ') || (str[i] == '.'))
{
space++;
}
}
for (i = 0, j = 0, k = 0; j < strlen(str); j++)
{
if ((str[j] == ' ') || (str[j] == 44) || (str[j] == 46))
{
p[i][k] = '\0';
i++;
k = 0;
}
else
p[i][k++] = str[j];
}
k = 0;
for (i = 0; i <= space; i++)
{
for (j = 0; j <= space; j++)
{
if (i == j)
{
strcpy(ptr1[k], p[i]);
k++;
count++;
break;
}
else
{
if (strcmp(ptr1[j], p[i]) != 0)
continue;
else
break;
}
}
}
for (i = 0; i < count; i++)
{
for (j = 0; j <= space; j++)
{
if (strcmp(ptr1[i], p[j]) == 0)
c++;
}
printf("%s : %d times.\n", ptr1[i], c);
c = 0;
}
}

最佳答案

暴力方法。时间复杂度可能是 O(N^2)。大约分为四个步骤:
1.输入循环('\n'将完成一个字符串输入)
2. 将字符串行拆分为单词列表。
3. 计算列表中每个单词的时间。 (暴力破解)
4. 打印结果。

#include <stdio.h>

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

#define MAXSTRLEN 1024

char **splitToList(char *a, int *rowSize) {
char **b=NULL;
int start=0;
int count=0;
for (int i = 0; i < strlen(a); ++i) {
if (' ' == a[start]) {
start = i;
continue;
}

if (' ' == a[i]) {
++count;
if (NULL == b) {
b = (char **)malloc(sizeof(char *));
b[count-1] = (char *)malloc(sizeof(char)*(i-start+1));
memcpy(b[count-1], &a[start], (i-start));
b[count-1][i-start] = '\0';
start = i;
} else {
b = (char **)realloc(b, sizeof(char *)*(count));
b[count-1] = (char *)malloc(sizeof(char)*(i-start+1));
memcpy(b[count-1], &a[start], (i-start));
b[count-1][i-start] = '\0';
start = i;
}
continue;
}

}

if (start <= strlen(a) && a[strlen(a)-1] != ' ') {
++count;
b = (char **)realloc(b, sizeof(char *)*(count));
b[count-1] = (char *)malloc(sizeof(char)*(strlen(a)-start+1));
memcpy(b[count-1], &a[start], (strlen(a)-start));
b[count-1][strlen(a)-start] = '\0';
}

*rowSize = count;

return b;
}

bool equalChars(char *a, char *b) {
if (strlen(a) != strlen(b)) {
return false;
}

int i=0;
while (i<strlen(a) && a[i] == b[i]) {
++i;
}

if (i < strlen(a)) {
return false;
}

return true;
}

void countWords(char *a) {
int *rowSize = (int *)malloc(sizeof(int));

// 1. split to word list
char **b = splitToList(a, rowSize);

// 2. count words times
int count[*rowSize];
for (int i = 0; i < *rowSize; ++i) {
if (0 == i) {
count[i] = 1;
continue;
}
int dup = false;
for (int j = 0; j <= i-1 ; ++j) {
if (equalChars(b[i], b[j])) {
count[j] = count[j] + 1;
count[i] = 0;
dup = true;
break;
}
}

if (!dup) {
count[i] = 1;
}
}

// 3. print words times
for (int i = 0; i < *rowSize; ++i) {
if (count[i] != 0) {
printf("word: %s, times: %d\n", b[i], count[i]);
}
}
}

int main(void) {

char *a = (char *)malloc(MAXSTRLEN);
int curLen = -1;

// input loop
int c;
for(;;) {
c = fgetc(stdin);
if(c == EOF) {
break;
}

if (curLen > MAXSTRLEN - 1) {
break;
}

if(c == '\n') {
a[++curLen] = '\0';
countWords(a);
curLen=-1;
} else {
a[++curLen] = (char) c;
}
}

}

关于c - 如何编写一个程序,让用户输入尽可能多的字符串并统计出现次数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58386505/

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