黑马程序员技术交流社区
标题:
1.2 关于函数方面基础习题含答案
[打印本页]
作者:
wangxiaoit
时间:
2014-12-17 16:59
标题:
1.2 关于函数方面基础习题含答案
习题4:
定义一个函数,其参数是一个字符串,返回该字符串中的单词数(单词以空格或标点符号来分割。假设字符串不含单双引号,也就是说没有isn't这样的单词)。定义第二个函数,它的第一个参数是一个字符串,第二个参数是一个数组,该函数将第一个字符串参数分割成单词,把这些单词存储在第二个数组参数中,最后返回存储在数组中的单词。
定义第三个函数,其参数是一个字符串,返回该字符串中的字母数。
使用这些函数实现一个程序,从键盘读入含有文本的字符串,输出文本中的所有单词,输出顺序是按照单词中的字母数,由短到长。
/* Exercise 8.4 A function to return the number of words in a string passed as an argument */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define LENGTH_INCREMENT 20
#define MAX_WORD_LENGTH 30
int countwords(char str[]); /* Function to count words in a string */
int segment_string(char str[], char *words[]); /* Function to segment a string */
int count_letters(char str[]); /* Function to count letters in a word */
int main(void)
{
char* pStr = NULL; /* Points to memory that stores the string */
char* pTemp = NULL; /* Temporary pointer */
char** words = NULL; /* Pointer to an array of words */
int length = 0; /* Current string length */
int max_length = 0; /* Current maximum string length */
int wordcount = 0; /* Count of words in string */
/* Read the string */
printf("Enter a string:\n");
do
{ /* If there is not enough memory, allocate some more */
if(length >= max_length)
{
max_length += LENGTH_INCREMENT;
pTemp = (char*)malloc(max_length);
if(pStr) /* If we have already read characters */
{ /* copy them to the new memory */
for(int i = 0 ; i<length ; i++)
pTemp[i] = pStr[i];
free(pStr);
}
pStr = pTemp;
pTemp = NULL;
}
}while((pStr[length++] = getchar()) != '\n');
pStr[--length] = '\0'; /* Append string terminator */
wordcount = countwords(pStr); /* Get the word count */
/* Now allocate memory to hold the words */
words = (char**)malloc(wordcount*sizeof(char*));
for(int i = 0 ; i<wordcount ; i++)
words[i] = (char*)malloc(MAX_WORD_LENGTH);
segment_string(pStr, words); /* Segment the string into words */
/* Sort the words in order of length */
for(int i = 0 ; i<wordcount-1 ; i++)
for(int j = i ; j<wordcount ; j++)
if(count_letters(words[i]) > count_letters(words[j]))
{
pTemp = words[i];
words[i] = words[j];
words[j] = pTemp;
}
/* Output the words and free the memory */
printf("The words in the string in order of length are:\n");
for(int i = 0 ; i<wordcount ; i++)
{
printf("%s\n",words[i]);
free(words[i]);
}
free(words); /* Free the memory for the pointers */
return 0;
}
/* Function to count words in a string */
int countwords(char str[])
{
int count = 0;
int i = 0;
while(str[i] != '\0')
{
if(!isalpha(str[i]))
{
i++;
continue;
}
++count;
while(isalpha(str[++i]))
;
}
return count;
}
/* Function to segment a string into words and return he number of words */
int segment_string(char str[], char *words[])
{
int count = 0;
int i = 0;
int j = 0;
while(str[i] != '\0')
{
if(!isalpha(str[i]))
{
i++;
continue;
}
j = 0;
while(isalpha(str[i]))
{
words[count][j++] = str[i++];
}
words[count++][j] = '\0';
}
return count;
}
/* Function to count letters in a word */
int count_letters(char str[])
{
int count = 0;
int i = 0;
while(str[i] != '\0')
{
if(isalpha(str[i++]))
++count;
}
return count;
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2