A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© wangxiaoit 中级黑马   /  2014-12-17 16:59  /  595 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

习题4:
定义一个函数,其参数是一个字符串,返回该字符串中的单词数(单词以空格或标点符号来分割。假设字符串不含单双引号,也就是说没有isn't这样的单词)。定义第二个函数,它的第一个参数是一个字符串,第二个参数是一个数组,该函数将第一个字符串参数分割成单词,把这些单词存储在第二个数组参数中,最后返回存储在数组中的单词。
定义第三个函数,其参数是一个字符串,返回该字符串中的字母数。
使用这些函数实现一个程序,从键盘读入含有文本的字符串,输出文本中的所有单词,输出顺序是按照单词中的字母数,由短到长。
  1. /* Exercise 8.4 A function to return the number of words in a string passed as an argument */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <ctype.h>

  5. #define LENGTH_INCREMENT  20
  6. #define MAX_WORD_LENGTH   30

  7. int countwords(char str[]);                                    /* Function to count words in a string */
  8. int segment_string(char str[], char *words[]);                 /* Function to segment a string        */
  9. int count_letters(char str[]);                                 /* Function to count letters in a word */


  10. int main(void)
  11. {
  12.    char* pStr = NULL;             /* Points to memory that stores the string */
  13.    char* pTemp = NULL;            /* Temporary pointer                       */
  14.    char** words = NULL;           /* Pointer to an array of words            */
  15.    int length = 0;                /* Current string length                   */
  16.    int max_length = 0;            /* Current maximum string length           */
  17.    int wordcount = 0;             /* Count of words in string                */

  18.    /* Read the string */
  19.    printf("Enter a string:\n");
  20.    do
  21.    { /* If there is not enough memory, allocate some more */
  22.      if(length >= max_length)
  23.      {
  24.        max_length += LENGTH_INCREMENT;
  25.        pTemp = (char*)malloc(max_length);
  26.        if(pStr)          /* If we have already read characters */
  27.        {                 /* copy them to the new memory        */
  28.          for(int i = 0 ; i<length ; i++)
  29.            pTemp[i] = pStr[i];
  30.          free(pStr);
  31.        }
  32.        pStr = pTemp;
  33.        pTemp = NULL;
  34.      }
  35.    }while((pStr[length++] = getchar()) != '\n');
  36.    pStr[--length] = '\0';         /* Append string terminator */

  37.    wordcount = countwords(pStr);  /* Get the word count       */

  38.    /* Now allocate memory to hold the words */
  39.    words = (char**)malloc(wordcount*sizeof(char*));
  40.    for(int i = 0 ; i<wordcount ; i++)
  41.      words[i] = (char*)malloc(MAX_WORD_LENGTH);

  42.    segment_string(pStr, words);   /* Segment the string into words */

  43.    /* Sort the words in order of length */
  44.    for(int i = 0 ; i<wordcount-1 ; i++)
  45.      for(int j = i ; j<wordcount ; j++)
  46.        if(count_letters(words[i]) > count_letters(words[j]))
  47.        {
  48.          pTemp = words[i];
  49.          words[i] = words[j];
  50.          words[j] = pTemp;
  51.        }
  52.    /* Output the words and free the memory */
  53.    printf("The words in the string in order of length are:\n");
  54.    for(int i = 0 ; i<wordcount ; i++)
  55.    {
  56.      printf("%s\n",words[i]);
  57.      free(words[i]);
  58.    }
  59.    free(words);      /* Free the memory for the pointers */
  60.    return 0;
  61. }

  62. /* Function to count words in a string */
  63. int countwords(char str[])
  64. {
  65.   int count = 0;
  66.   int i = 0;
  67.   while(str[i] != '\0')
  68.   {
  69.     if(!isalpha(str[i]))
  70.     {
  71.       i++;
  72.       continue;
  73.     }
  74.     ++count;
  75.     while(isalpha(str[++i]))
  76.       ;
  77.   }
  78.   return count;
  79. }

  80. /* Function to segment a string into words and return he number of words */
  81. int segment_string(char str[], char *words[])
  82. {
  83.   int count = 0;
  84.   int i = 0;
  85.   int j = 0;
  86.   while(str[i] != '\0')
  87.   {
  88.     if(!isalpha(str[i]))
  89.     {
  90.       i++;
  91.       continue;
  92.     }
  93.     j = 0;
  94.     while(isalpha(str[i]))
  95.     {
  96.       words[count][j++] = str[i++];
  97.     }
  98.     words[count++][j] = '\0';
  99.   }
  100. return count;
  101. }

  102. /* Function to count letters in a word */
  103. int count_letters(char str[])
  104. {
  105.   int count = 0;
  106.   int i = 0;
  107.   while(str[i] != '\0')
  108.   {
  109.     if(isalpha(str[i++]))
  110.       ++count;
  111.   }
  112.   return count;
  113. }
复制代码




0 个回复

您需要登录后才可以回帖 登录 | 加入黑马