【问题标题】:Sorting word occurrence in string C programming对字符串 C 编程中出现的单词进行排序
【发布时间】:2015-10-23 04:59:38
【问题描述】:

我已经坚持了一段时间了。我编写了我的程序来计算用户输入字符串中单词的出现次数,并按字母顺序对单词进行排序。我的问题是我的程序只有在输入的单词之间有空格时才能完美运行。例如,如果我输入“to to”,由于逗号,我的程序会将这两个单词计为两个不同的单词,而不是像我希望的那样将它计为“to”中的一个单词。这是我在数组const char delim[] 中的所有分隔符的问题。如何在我的程序中解决此问题?我真的很感激任何帮助!我的代码如下:

编辑:我采纳了 Bob 的建议来使用 strchr(),它奏效了!我唯一的问题是我的程序现在输出分隔符的计数。在比较words[i]words[j] 以查看它们是否具有相同的值时,我正在考虑可能编写一个 if 语句。这是最好的方法吗?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
const char delim[] = ", . - !*()&^%$#@<> ? []{}\\ / \"";
#define SIZE 1000

int main(){
    char string[SIZE], words[SIZE][SIZE], temp[SIZE];

    int a = 0, i = 0, j = 0, k = 0, n = 0, count;
    int c = 0, cnt[26] = { 0 };
    int word = 0;
    int x;
    printf("Enter your input string:");
    fgets(string, SIZE, stdin);
    string[strlen(string) - 1] = '\0';
    lower(string);
    /*extracting each and every string and copying to a different place */
    while (string[i] != '\0'){
        if (strchr(", . - !*()&^%$#@<> ? []{}\\ / \"", string[i]) != NULL){
            words[j][k] = '\0';
            k = 0;
            j++;
        }else   {
            words[j][k++] = string[i];
        }
        i++;
    }

    words[j][k] = '\0';
    n = j;

    printf("Number of occurences of each word unsorted:\n");
    i = 0;
    /* find the frequency of each word and print the results */
    while (i <= n) {
        count = 1;
        if (i != n) {
            for (j = i + 1; j <= n; j++) {
                if (strcmp(words[i], words[j]) == 0) {
                    count++;
                    for (a = j; a <= n; a++)
                        strcpy(words[a], words[a + 1]);
                        n--;
                }
            }//for
        }
        //word == strtok(string, delim);
        /* count - indicates the frequecy of word[i] */
        printf("%s\t%d\n", words[i], count);
        i = i + 1;
    }//while
    printf("Alphabetical Order:\n");
    /* sort the words in the given string */
    for (i = 0; i < n; i++){
        strcpy(temp, words[i]);
        for (j = i + 1; j <= n; j++){
            if (strcmp(words[i], words[j]) > 0){
                strcpy(temp, words[j]);
                strcpy(words[j], words[i]);
                strcpy(words[i], temp);
            }
        } //inner for
    }  //outer for
    i = 0;
    while (i <= n) {
        count = 1;
        if (i != n) {
            for (j = i + 1; j <= n; j++) {
                if (strcmp(words[i], words[j]) == 0) {
                    count++;
                }
            }
        }

        printf("%s\n", words[i]);
        i = i + count;
    }
}

【问题讨论】:

  • 你听说过函数/方法吗?
  • @MitchWheat 是的,我知道为什么?另外,听说过 strtok,但我不知道如何在这里使用它。
  • 那么我强烈建议使用它们...
  • 会不会是因为@MitchWheat 的代码格式更好?
  • Omar,你的代码很乱而且难以阅读,这使得另一个 SO 用户很难进入那里并尝试将小麦(@MitchWheat 上没有双关语)与谷壳分开。

标签: c string


【解决方案1】:

在比较之前去掉该分隔符的每个单词。实际上,您甚至不需要分隔符列表,因为除了它是分隔符之外,单词是“alpha”。

【讨论】:

  • 我为此创建了数组 delim[] 但我不知道该怎么做。我尝试设置 string[i] = delim 但这不起作用。
  • 在函数中使用isalpha函数判断一个字符串是否非alpha
  • 哦,好吧,所以将第一个 while 循环中第一个 if 语句中的 string[i] = ' ' 替换为 !isalphastring[i]?
  • 确保 isalpha 接受一个字符而不是字符串,问题解决。
  • 我现在编译了我的程序进行编辑,但它仍然不能算作“to to”示例输入中的两个单词
【解决方案2】:

请试试这个,它有效,它是你自己的代码的摘录,稍加修改,它会给你计数,然后你必须写剩下的,玩得开心。

#include <string.h>
#define YES 1
#define NO 0

int main(  )
{
  char string[1000];
  int i = 0;
  int j = 0;
  int is_this_a_word = 0;

  strcpy( string, " to or not ,tobe" );

  while ( string[i++] != '\0' )
  {
    if ( strchr( ", . - !*()&^%$#@<> ? []{}\\ / \"", string[i] ) != NULL )
    {
      is_this_a_word = NO;
    }
    else
    {
      if ( ! is_this_a_word )
      {
    is_this_a_word = YES;
    j++;
      }
    }
  }

  printf( "I counted %d words", j );
  getchar(  );
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-31
    • 1970-01-01
    • 1970-01-01
    • 2014-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多