计算字符串中小写字母词的数量-程序园

时间:2019-01-31 13:25:38

标签: c string

我需要一些帮助来编写一个函数,该函数将计算字符串中小字母单词的数量。

出于此分配的目的,“单词”被定义为唯一的小写字母(在char >= 'a'char =< 'z'之间)的序列,没有任何其他字符

int numberOfSmallLetterWords(char* str)

例如:

numberOfSmallLetterWords(" it is a sMall World after all! ")

返回值:4

“全部!”不考虑单词

2 个答案:

答案 0 :(得分:0)

int all_lower(const char *str)
{
    while(*str)
       if(!islower(*str++)) return 0;
    return 1;
}

size_t wlcount(const char *str, const char *del)
{
    size_t count = 0;
    char *copy = strdup(str);
    char *word;

    if(copy)
    {
        word = strtok(copy, (char *)del);
        while(word)
        {
            if(*word) count += all_lower(word);
            word = strtok(NULL, del);
        }
        free(copy);
    }
    return count;
}

int main()
{
    printf("%zu\n", wlcount(" it is a sMall World after all! ", " !"));
}

答案 1 :(得分:-1)

一个提案,非常快,因为该字符串仅运行一次,并且堆中没有动态(取消)分配。查看代码中的注释:

#include <stdio.h>
#include <ctype.h>

int numberOfSmallLetterWords(char* str)
{
  int nWords = 0;
  int inWord = 0;
  int inSpaces = 1; /* not 0 to allow a word to start at the first character */

  while (*str) {
    if (islower(*str)) {
      /* compatible with a word */
      if (inSpaces) {
        /* start a potential new word */
        inSpaces = 0;
        inWord = 1;
      }
      /* else nothing change:
         - if we was in a word we are still
         - if we was not in word nor spaces we are still in none of them 
      */
    }
    else if (isspace(*str)) {
      if (inWord) {
        /* spaces after a potential word validates it */
        nWords += 1;
        inWord = 0;
      }
      inSpaces = 1;
    }
    else
      /* not in spaces nor in word */
      inWord = inSpaces = 0;
    str += 1;
  }

  /* warning do not forget to count a possible word at the end */
  return (inWord) ? (nWords + 1) : nWords;
}

int main()
{
  printf("%d\n", numberOfSmallLetterWords(" it is a sMall World after all! "));
  return 0;
}

执行:

4

在valgrind下:

==9376== Memcheck, a memory error detector
==9376== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==9376== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==9376== Command: ./a.out
==9376== 
4
==9376== 
==9376== HEAP SUMMARY:
==9376==     in use at exit: 0 bytes in 0 blocks
==9376==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==9376== 
==9376== All heap blocks were freed -- no leaks are possible
==9376== 
==9376== For counts of detected and suppressed errors, rerun with: -v
==9376== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 6)

如果您不想使用 ctype islower isspace

int islower(char c)
{
  return ((c >= 'a') && (c <= 'z')); 
}

int isspace(char c)
{
  switch (c) {
  case ' ':
  case '\t':
  case '\n':
  case '\r':
    /* may be more cases */
    return 1;
  default:
    return 0;
  }
}

警告这些定义不如ctype版本具有可移植性