2014-07-24 2 views
0

나는 문장의 각 단어에서 모음 및 문자를 계산하는 방법을 알아 내려고 노력해 왔습니다. 예를문장의 각 단어에서 모음 및 문자 계산하기

를 들어 문장

hello : 5 characters, 2 vowels

there : 5 characters, 2 vowelshello there합니다. 나는 완전한 문장을 위해 같은 일을하는 코드를 보았다. 그러나 한 마디로 한마디도하지 말라. 다음은

는 입력이 모두 lower case 될 것입니다 내가

int main() { 
    char str[512] = "hello there", word[256]; 
    int i = 0, j = 0, v, h; 
    str[strlen(str)] = '\0'; 

    /* checking whether the input string is NULL */ 
    if (str[0] == '\0') { 
     printf("Input string is NULL\n"); 
     return 0; 
    } 

    /* printing words in the given string */ 
    while (str[i] != '\0') { 
     /* ' ' is the separator to split words */ 
     if (str[i] == ' ') 
     { 
      for (h = 0; word[h] != '\0'; ++h) 
      { 
       if (word[h] == 'a' || word[h] == 'e' || word[h] == 'i' || word[h] == 'o' || word[h] == 'u')++v; 
      } 
      printf("\nVowels: %d", v); 
      word[j] = '\0'; 

      printf("%s\n", word); 
      j = 0; 
     } 
     else 
     { 
      word[j++] = str[i]; 
     } 
     i++; 
    } 

    word[j] = '\0'; 

    /* printing last word in the input string */ 
    printf("%s\n", word); 
    return 0; 
} 

작업했던 코드입니다. 나는 이것을 알아 내는데 어려움을 겪고있다.

코드를 실행하는 동안 모음 수가 많지 않습니다. 나는 그 문장을 나눌 수있다. 그러나 모음 카운팅은 일어나지 않습니다.

답변

3

한 매우 간단한 방법 :

#include <stdio.h> 

const char* s(int n) 
{ 
    return n == 1? "" : "s"; 
} 

void count (const char* str) 
{ 
    for (int i = 0;;) 
     for (int v = 0, w = i;;) 
     { 
      int len; 
      char c = str[i++]; 
      switch (c) 
      { 
      case 'a': case 'e': case 'i': case 'o': case 'u': 
       v++; 
      default: 
       continue; 
      case ' ': case '\t': case '\n': case '\0': 
       len = i - 1 - w; 
       printf("'%.*s': %d character%s, %d vowel%s\n", len, str+w, len, s(len), v, s(v)); 
       if (c) 
        break; 
       else 
        return; 
      } 
      break; 
     } 
} 


int main(void) 
{ 
    count("My words with vowels"); 
    return 0; 
} 
+0

의 완벽한 ** 작동 * *. 단어의 문자를 세는 코드는 어디에 넣을 수 있습니까? 두 번째'사례'자체에서? –

+1

@smashIT 해당 printf의 첫 번째 표현식은 단어 길이입니다. 사실, 그것을 단순화하기 위해 편집 할 것입니다. –

+0

문자의 숫자를 인쇄 할 때, 마지막 단어에 대해 +1 숫자입니다. –

0

아마도 할 수있는 일은 ""(공백)이 발생할 때 문자와 모음의 수를 인쇄 한 다음 카운터를 재설정하는 것입니다. 그렇게하면 문장의 각 단어에 대한 문자와 모음을 찾을 수 있습니다.

0

문장 전체에서이 작업을 수행하는 논리를 이해하면 문장을 개별 단어로 분리하고 각 단어에 동일한 논리를 적용하여 단일 단어로도 수행 할 수 있습니다. 문장을 단어로 분해하기 위해 단어를 공백 (또는 복수, 아마도)으로 구분한다는 사실을 사용할 수 있습니다.

1

이 코드를 사용해보십시오. 그것은

는 여기에 몇 가지 의사 -code <입니다 .. 당신이

#include<stdio.h> 
int main() { 
    char str[512] = "hello there", word[256]; 
    int i = 0, j = 0, v=0,h; // you didn't initialize v to 0 
    str[strlen(str)] = '\0'; 

    /* checking whether the input string is NULL */ 
    if (str[0] == '\0') { 
      printf("Input string is NULL\n"); 
      return 0; 
    } 

    /* printing words in the given string */ 
    while (str[i] != '\0') { 
      /* ' ' is the separator to split words */ 
      if (str[i] == ' ') { 
    for (h = 0; word[h] != '\0'; h++) { 
    if (word[h] == 'a' || word[h] == 'e' || word[h] == 'i' || word[h] == 'o' || word[h] == 'u') 
      v++; 
    } 
    printf("%s :", word); 
    printf(" %d chracters,",strlen(word)); 
    printf(" %d Vowels.\n", v); 

    j = 0; v=0; 
    word[j] = '\0'; 
    } else { 
      word[j++] = str[i]; 
      word[j] = '\0'; 
    } 
    i++; 
    } 

    /* calculating vowels in the last word*/ // when NULL occurs, Wont enter into while loop. 
    for (h = 0; word[h] != '\0'; h++) { 
    if (word[h] == 'a' || word[h] == 'e' || word[h] == 'i' || word[h] == 'o' || word[h] == 'u') 
    v++; 
    } 
    printf("%s :", word); 
    printf(" %d chracters,",strlen(word)); 
    printf(" %d Vowels.\n", v); 

    return 0; 
} 
1

이 숙제처럼 엄청 많이 소리 도움이 될 수 있습니다 - 아래와 같이 실행되지 않습니다. 그냥 논리를 보여줍니다. 이 V의 + C를 실현하는 것은 당신에게 등 인쇄 총 워드 길이를 줄 것 같은 분 세부 있다는 넘어

int c = 0; 
int v = 0; 
for (int i = 0; i < lengthOfSentence; i++){ 
    if (stringName[i] == '\0') { //optionally '\n' may be more suitable 
     return; 
    } 
    if (stringName[i] == ' '){ 
     print previousWord // + c, v in whatever format you want 
     c = 0; 
     v = 0; 
    } 
    if (stringName[i] == vowel) { //you can do this part like in your code 
     word[v+c] = stringName[i]; //get current char and add to next slot 
     v++; 
    } 
    else { 
     word[v+c] = stringName[i]; 
     c++; 
    } 

..

관련 문제