C语言 分析每个单词

kr98yfug  于 2022-12-11  发布在  其他
关注(0)|答案(2)|浏览(114)

我的程序读取一个txt文件,可以将所有内容一个接一个地打印到控制台,但我必须将每个变量存储在不同的字符串中,然后将它们分别打印到控制台。
预期输出为:

///////////
First word: Grep
Second word: danger
Third word: <
////////////  
First word: ls
Second word: -a
Third word : -
/////////

程序的输出:

grep
 danger
 <
ls
-a
-

输入文件内容:
第一次

lyr7nygr

lyr7nygr1#

我建议你创建一个循环,一次读取和处理一行。为了一次读取一行,你可以使用函数fgets。这比使用fgetc一次阅读一个字符要容易。
您可以定义一个数组,将数字110Map到各个字符串文字"First""Second""Third"等:

const char *map[] = {
    "Zeroth",
    "First", "Second", "Third", "Fourth", "Fifth",
    "Sixth", "Seventh", "Eighth", "Ninth", "Tenth"
};

现在您可以更改行

while( token != NULL ) {
    printf( " %s\n", token );
    token = strtok(NULL, s);
}

至:

for ( int i = 1; i < 11 && token != NULL; i++ ) {
    printf( "%s word: %s\n", map[i], token );
    token = strtok(NULL, s);
}

这样,它将打印:

First word: Grep
Second word: danger
Third word: <

进行这些更改后,程序应如下所示:

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

#define NUM_MAPPINGS 11

int main( void )
{
    FILE *file;
    char line[200];
    char *token;
    const char *delimiter = " ";
    const char *separator = "///////////";

    const char *map[NUM_MAPPINGS] = {
        "Zeroth",
        "First", "Second", "Third", "Fourth", "Fifth",
        "Sixth", "Seventh", "Eighth", "Ninth", "Tenth"
    };

    //attempt to open file
    file = fopen( "commands.txt", "r" );
    if ( file == NULL )
    {
        fprintf( stderr, "Error opening file!\n" );
        exit( EXIT_FAILURE );
    }

    //print separator
    printf( "%s\n", separator );

    //process one line per loop iteration
    while ( fgets( line, sizeof line, file ) != NULL )
    {
        //remove newline character from input, if it exists
        line[strcspn(line,"\n")] = '\0';

        //find first token
        token = strtok( line, delimiter );

        for ( int i = 1; i < NUM_MAPPINGS && token != NULL; i++ )
        {
            //print current token
            printf( "%s word: %s\n", map[i], token );

            //find next token
            token = strtok( NULL, delimiter );
        }

        //print separator
        printf( "%s\n", separator );
    }

    //cleanup
    fclose( file );
    
    return 0;
}

对于问题中指定的输入,此程序具有以下输出:

///////////
First word: grep
Second word: danger
Third word: <
///////////
First word: ls
Second word: -a
Third word: wc
Fourth word: hw2
Fifth word: .
Sixth word: c
Seventh word: >
///////////
flseospp

flseospp2#

答得好......
我把它扩展了一点:)

#include <stdio.h>
#include <stdint.h>

void my_print(int num, char *s)
{
  char level_0[19][12] =
  {
    "First",
    "Second",
    "Third",
    "Forth",
    "Fifth",
    "Sixth",
    "Seventh",
    "Eighth",
    "Nineth",
    "Tenth",
    "Eleventh",
    "Twelveth",
    "Thirteenth",
    "Forteenth",
    "Fifteenth",
    "Sixteenth",
    "Seventeenth",
    "Eighteenth",
    "Nineteeth"
  };

  char level_1[8][7] =
  {
    "Twent",
    "Thirt",
    "Fort",
    "Fift",
    "Sixt",
    "Sevent",
    "Eight",
    "Ninet"
  };

  if (num < 20)
  {
    printf("%s word: %s\n", level_0[num - 1], s);
  }

  else if (num % 10 == 0)
  {
    printf("%sieth word: %s\n", level_1[num/10 - 2]);
  }

  else
  {
    printf("%sy-%s word: %s\n", level_1[num/10 - 2], level_0[num%10 - 1], s);
  }

}

int get_token(FILE *fp, char *s, int max_length)
{
  char c;
  int counter = 0;

  s[0] = '\0';

  while (!feof(fp))
  {
    if (counter == max_length) { return -2; }

    c = getc(fp);

    if (c == ' ') { return 1; }
    if (c == '\n') { return 2; }
    *s = c;
    s++;
    *s = '\0';
    counter++;
  }

  return -1;
}

int main(int argc, char *argv[])
{
  if (argc != 2)
  {
    printf("Arguments Error.\nUsage: parse_words [filename].\n");
    
    return 1;
  }

  const char *fname = argv[1];
  FILE *fp;
  char token[80];
  size_t len = 80;
  int word_count = 0;
  int get_token_state;

  fp = fopen(fname, "r");

  if (fp == NULL)
  {
    printf("Error opening file %s!\n", fname);
    
    return 1;
  }

  printf("///////////\n");

  while ((get_token_state = get_token(fp, token, len)) != -1)
  {
    if (get_token_state == -2)
    {
      printf("Warning, overflow near %s\n.", token);
    }

    else if (get_token_state == 1)
    {
      word_count++;
      my_print(word_count, token);
    }

    else if (get_token_state == 2)
    {
      word_count++;
      my_print(word_count, token);
      word_count = 0;
      printf("///////////\n");
    }
  }

  fclose(fp);

  return 0;
}

相关问题