鱼C论坛

 找回密码
 立即注册
查看: 2397|回复: 3

一个纠结的字符串问题

[复制链接]
发表于 2012-1-19 23:52:25 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
/* Program 6.10 Analyzing text */
#include <stdio.h>
//#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#define TEXTLEN  10000      /* Maximum length of text            */
#define BUFFERSIZE 100      /* Input buffer size                 */
#define MAXWORDS   500      /* Maximum number of different words */
#define WORDLEN     15      /* Maximum word length               */
int main(void)
{
char text[TEXTLEN+1];
  char buffer[BUFFERSIZE];
  char endstr[] = "*\n";          /* Signals end of input       */
  const char space = ' ';
  const char quote = '\'';
  char words[MAXWORDS][WORDLEN+1];
  int nword[MAXWORDS];            /* Number of word occurrences */
  char word[WORDLEN+1];           /* Stores a single word       */
  int wordlen = 0;                /* Length of a word           */
  int wordcount = 0;              /* Number of words stored     */

  printf("Enter text on an arbitrary number of lines.");
  printf("\nEnter a line containing just an asterisk to end input:\n\n");
  /* Read an arbitrary number of lines of text */
  while(true)
  {
    /* A string containing an asterisk followed by newline */
    /* signals end of input                                */
    if(!strcmp(fgets(buffer, BUFFERSIZE, stdin), endstr))
      break;
    /* Check if we have space for latest input */
  if(strlen(text)+strlen(buffer)+1 > TEXTLEN)(我用的VC 2010 比如 我输入worinima 回车  (此时居然就解决了显示maximum....)问题来了怎么可能满足这个条件呢?明明就不大于, VC6.0 我群里说的输入worinima 回车后 输入* 就结束显示我输入的字符串的数目 为什么VC2010不行了? 还是这个程序有矛盾?
      {
        printf("Maximum capacity for text exceeded. Terminating program.");
        return 1;
      }
    strcat(text, buffer);
  }
  /* Replace everything except alpha and single quote characters by spaces */
  for(int i = 0 ; i < strlen(text) ; i++)
  {
    if(text[i] == quote || isalnum(text[i]))
      continue;
    text[i] = space;
  }
  /* Find unique words and store in words array */
  int index = 0;
  while(true)
  {
    /* Ignore any leading spaces before a word */
    while(text[index] == space)
      ++index;
    /* If we are at the end of text, we are done */
    if(text[index] == '\0')
      break;
    /* Extract a word */
    wordlen = 0;          /* Reset word length */
    while(text[index] == quote || isalpha(text[index]))
    {
      /* Check if word is too long */
      if(wordlen == WORDLEN)
      {
        printf("Maximum word length exceeded. Terminating program.");
        return 1;
      }
      word[wordlen++] = tolower(text[index++]);  /* Copy as lowercase     */
    }
    word[wordlen] = '\0';                        /* Add string terminator */
    /* Check for word already stored */
    bool isnew = true;
    for(int i = 0 ; i< wordcount ; i++)
      if(strcmp(word, words[i]) == 0)
      {
        ++nword[i];
        isnew = false;
        break;
      }
    if(isnew)
    {
      /* Check if we have space for another word */
      if(wordcount >= MAXWORDS)
      {
        printf("\n Maximum word count exceeded. Terminating program.");
        return 1;
      }
      strcpy(words[wordcount], word);   /* Store the new word  */
      nword[wordcount++] = 1;           /* Set its count to 1  */
    }
  }
  /* Output the words and frequencies */
  for(int i = 0 ; i<wordcount ; i++)
  {
    if( !(i%3) )                       /* Three words to a line */
      printf("\n");
    printf("  %-15s%5d", words[i], nword[i]);
  }
    这个程序意思就是输入一个文本   最后显示输入的字母 数字的次数return 0;
}




                               
登录/注册后可看大图
该贴已经同步到 空手套小白狼的微博
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-1-20 00:06:44 | 显示全部楼层
我表示这么长的源代码没有耐心去看。。能不能把问题精简一下。。
小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2012-1-20 00:47:54 | 显示全部楼层
本来我的理解是输入一个字符串后回车 他此时不会结束程序 需要我输入* 才会结束 可是我一回车就自动结束了。
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-1-20 02:01:21 | 显示全部楼层
回车没用getchar()吸收
小甲鱼最新课程 -> https://ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-11-10 22:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表