7

文字列内の単語をカウントする関数を作成する必要があります。この割り当ての目的で、「単語」は、空白で他の単語から分離された、nullでも空白でもない文字のシーケンスとして定義されます。

これは私がこれまでに持っているものです:

int words(const char sentence[ ]);

int i, length=0, count=0, last=0;
length= strlen(sentence);

for (i=0, i<length, i++)
 if (sentence[i] != ' ')
     if (last=0)
        count++;
     else
        last=1;
 else
     last=0;

return count;

プログラム全体が終了するまでテストできず、機能するかどうかわからないため、機能するかどうかはわかりませんが、この関数を作成するためのより良い方法はありますか?

4

13 に答える 13

6

あなたが必要

int words(const char sentence[])
{
}

(中括弧に注意)。

for ループは;の代わりに使用し,ます。


免責事項がなければ、ここに私が書いたであろうものがあります:

ライブで見るhttp://ideone.com/uNgPL

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

int words(const char sentence[ ])
{
    int counted = 0; // result

    // state:
    const char* it = sentence;
    int inword = 0;

    do switch(*it) {
        case '\0': 
        case ' ': case '\t': case '\n': case '\r': // TODO others?
            if (inword) { inword = 0; counted++; }
            break;
        default: inword = 1;
    } while(*it++);

    return counted;
}

int main(int argc, const char *argv[])
{
    printf("%d\n", words(""));
    printf("%d\n", words("\t"));
    printf("%d\n", words("   a      castle     "));
    printf("%d\n", words("my world is a castle"));
}
于 2012-10-02T22:11:26.383 に答える
4

次の例を参照してください。次のアプローチに従うことができます:単語間の空白を数えます。

int words(const char *sentence)
{
    int count=0,i,len;
    char lastC;
    len=strlen(sentence);
    if(len > 0)
    {
        lastC = sentence[0];
    }
    for(i=0; i<=len; i++)
    {
        if((sentence[i]==' ' || sentence[i]=='\0') && lastC != ' ')
        {
            count++;
        }
        lastC = sentence[i];
    }
    return count;
}

テストする :

int main() 
{ 
    char str[30] = "a posse ad esse";
    printf("Words = %i\n", words(str));
}

出力:

Words = 4
于 2012-10-02T22:03:07.527 に答える
2
#include <ctype.h> // isspace()

int
nwords(const char *s) {
  if (!s) return -1;

  int n = 0;
  int inword = 0;
  for ( ; *s; ++s) {
    if (!isspace(*s)) {
      if (inword == 0) { // begin word
        inword = 1;
        ++n;
      }
    }
    else if (inword) { // end word
      inword = 0;
    }
  }
  return n;
}
于 2012-10-02T22:21:21.010 に答える
1

これが解決策の1つです。複数のスペースを含む単語、またはスペースまたはスペースの後に単語が続く単語をカウントします。

#include <stdio.h>
int main()
{
    char str[80];
    int i, w = 0;
    printf("Enter a string: ");
    scanf("%[^\n]",str);

    for (i = 0; str[i] != '\0'; i++)
    {
       
        if((str[i]!=' ' && str[i+1]==' ')||(str[i+1]=='\0' && str[i]!=' '))
        {
            w++;
        }
        
    }

    printf("The number of words = %d", w );

    return 0;
}
于 2021-01-31T16:47:34.103 に答える
0
#include<stdio.h>

int main()   
{    
char str[50];    
int i, count=1;  
printf("Enter a string:\n");    
gets(str);    
for (i=0; str[i]!='\0'; i++)   
        {
        if(str[i]==' ')    
                {
                count++;
                }
        }
printf("%i\n",count);    
}
于 2014-03-18T10:16:06.477 に答える
0
#include<stdio.h>
#include<string.h>

int getN(char *);


int main(){
    char str[999];
    printf("Enter Sentence: "); gets(str);
    printf("there are %d words", getN(str));
}


int getN(char *str){
    int i = 0, len, count= 0;
    len = strlen(str);
    if(str[i] >= 'A' && str[i] <= 'z')
       count ++;


    for (i = 1; i<len; i++)
        if((str[i]==' ' || str[i]=='\t' || str[i]=='\n')&& str[i+1] >= 'A' && str[i+1] <= 'z')
        count++;


return count;
}
于 2015-04-18T00:55:34.240 に答える
0

別の解決策は次のとおりです。

#include <string.h>

int words(const char *s)
{
    const char *sep = " \t\n\r\v\f";
    int word = 0;
    size_t len;

    s += strspn(s, sep);

    while ((len = strcspn(s, sep)) > 0) {
        ++word;
        s += len;
        s += strspn(s, sep);
    }
    return word;
}
于 2012-10-23T21:26:43.380 に答える
0
#include <stdio.h>

int wordcount (char *string){

    int n = 0; 

    char *p = string ;
    int flag = 0 ;

    while(isspace(*p)) p++;


    while(*p){
        if(!isspace(*p)){
            if(flag == 0){
                flag = 1 ;
                n++;
            }
        }
        else flag = 0;
        p++;
    }

    return n ;
}


int main(int argc, char **argv){

    printf("%d\n" , wordcount("    hello  world\nNo matter how many newline and spaces"));
    return 1 ;
}
于 2015-08-04T15:46:10.350 に答える
-1

これが1つの解決策です。これは、単語間に複数のスペースがあったり、句読記号の周りにスペースがなかったりしても、正しく単語を数えます。例: I am,My mother is. 象、飛び去ります。

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


int countWords(char*);


int main() {
    char string[1000];
    int wordsNum;

    printf("Unesi nisku: ");
    gets(string);  /*dont use this function lightly*/

    wordsNum = countWords(string);

    printf("Broj reci: %d\n", wordsNum);

    return EXIT_SUCCESS;
}


int countWords(char string[]) {
    int inWord = 0,
        n,
        i,
        nOfWords = 0;

    n = strlen(string);

    for (i = 0; i <= n; i++) {
        if (isalnum(string[i]))
            inWord = 1;
        else
            if (inWord) {
                inWord = 0;
                nOfWords++;
            }
    }

    return nOfWords;
}
于 2013-09-02T20:57:06.053 に答える
-1

これは、単語数を計算するためのより単純な関数です

int counter_words(char* a){`

 // go through chars in a
 // if ' ' new word
 int words=1;
 int i;
 for(i=0;i<strlen(a);++i)
 {
      if(a[i]==' ' && a[i+1] !=0)
      {
           ++words;
      }
 }

return words;}

于 2016-07-08T09:14:08.113 に答える