2014-02-15 43 views
0

我想寫一個函數來掃描字符串並找到大寫字母'O'的集合。說我的字符串是yyyOOOyyyOOyyyOyyy,它應該打印:嵌套循環C編程 - 我需要掃描輸入找到字母集

一組3個大寫字母O被發現在一起。

一組2個大寫字母O被發現在一起。

一組1個大寫字母O被發現在一起。

我找不到一個好辦法做到這一點。我試圖使用嵌套循環,但我還沒有足夠的經驗。這是我到目前爲止所提出的(我知道它完全不起作用)。我的代碼也不允許使用scanf的用戶輸入(我必須使用scanf進行分配)。在糾正我的循環和讓scanf工作方面的任何幫助都會很棒!謝謝!

void problem_03_function(){ 

    char double_o_string[30]; 
    scanf("%s", &double_o_string); 
    int count_double_o = 0; 
    char capital_letter_O = 'O'; 
    int num_in_str = 0; 

    for(num_in_str; num_in_str < strlen(double_o_string); num_in_str++){ 
      if(double_o_string[num_in_str] == capital_letter_O){ 
        count_double_o++; 
      } 
      printf("a group of %d capital letter O's have been found togeth$ 
      } 
} 
+0

你只是算'O'字符數。您需要跟蹤當前/前一個字符以檢測序列。 –

回答

-1

我想如果你把它放在你的循環中,它就會工作。

if(double_o_string[num_in_str] == capital_letter_O){ 
    count_double_o++; 
} 
else if(count_double_o != 0) { 
    printf("a group of %d capital letter O's have been found togeth", count_double_o) 
    count_double_o = 0; 
} 
+0

這是否處理'yyOOO'(字符串末尾的一系列'O')? –

+0

@JonathanLeffler試試吧...... – Nico

+0

我做到了;它忽略了最後的O組。你試過了嗎? –

0

你可能需要做這樣的事情:

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

int* findSet(char *p) { 

    char cTS = 'o'; 
    int i = 0; 
    int lastFound = 0; 

    int *array = malloc(sizeof(int) * 10); /* You can do better by allocating 
    dynamically because this will limit you to 10 sets only. */ 
    int count = 0; 
    int lastPos = 0; 
    int strle = strlen(p); 

    for(; i < strle; i++) { 
     if(p[i] == cTS) { 
      count++; 
      lastFound = 1; 
      if(i == strle - 1) { 
       if(lastPos >= 10) return array; 
       array[lastPos++] = count; 
       break; 
      } 
     } 
     else { 
      if(lastFound) { 
       if(lastPos >= 10) return array; 
       array[lastPos++] = count; 
      } 
      count = 0; 
      lastFound = 0; 
     } 
    } 

    for(; 10 - lastPos;) { 
     array [lastPos++] = 0; 
    } 

    return array; 
} 

int main() { 
    int *x = findSet("ohoHeloooloo"); 
    int i = 0; 
    for(; i < 10; i++) { 
     printf("%d", x[i]); 
    } 
    return 0; 
} 

請注意,您可以更改要通過更改cTS變量來搜索內容。

0

試試這個:

void problem_03_function(){ // homework? ;) 

    char double_o_string[30]; 
    scanf("%s", double_o_string); // Without & as double_o_string is already a pointer. See http://stackoverflow.com/questions/5406935/ 
    int count_double_o = 0; 
    char capital_letter_O = 'O'; 
    int num_in_str = 0; 
    int notice_not_printed = 0; // EDIT: simplified version of @kkaushi's answer 

    for(; num_in_str < strlen(double_o_string); num_in_str++){ // You don't really need the first statement of for statement here 
     if(double_o_string[num_in_str] == capital_letter_O){ 
      count_double_o++; 
      notice_not_printed = 1; 
     } else if (count_double_o) { // to prevent printing "a group of 0 capital..." 
      printf("a group of %d capital letter O's have been found together\n", count_double_o); 
      count_double_o = 0; // you need to reset the capital O count 
      notice_not_printed = 0; 
     } 
    } 
    // Used if the string ends with 'O'. See @kkaushi's answer 
    if (notice_not_printed) 
     printf("a group of %d capital letter O's have been found together\n", count_double_o); 
} 
0
void show_fn(){ 
    char double_o_string[30]; 
    scanf("%s", double_o_string); 
    int count_double_o = 0; 
    int flag=0; 
    char capital_letter_O = 'O'; 
    int num_in_str = 0; 
    for(num_in_str; num_in_str < strlen(double_o_string); num_in_str++){ 
     if(double_o_string[num_in_str] == capital_letter_O){ 
      flag=1; 
      count_double_o++; 
      if(num_in_str+1==strlen(double_o_string)){printf("a group of %d capital letter O's have been found together\n",count_double_o);} 
    }else{ 
      if(flag==1){ 
        printf("a group of %d capital letter O's have been found together\n",count_double_o); 
        count_double_o=0;flag = 0; 
      } 
     } 
    } 
} 
+1

@Timothy_G當輸入以O結尾時,您的解決方案會錯過 – kkaushi

0
void problem_03_function(){ 
    const char capital_letter_O = 'O'; 
    char double_o_string[30]; 
    scanf("%s", &double_o_string); 

    int i = 0, o_gp= 0, o_gp_flag[3] = {0}; 
    int len = strlen(double_o_string); 

    while(i < len){ 
     o_gp = 0; 
     while(double_o_string[i++] == capital_letter_O){ 
      if(++o_gp == 3 || i == len)break; 
     }//when o_gp > 3 , skip ? 
     if(o_gp) 
      o_gp_flag[o_gp-1] = 1; 
    } 
    for(i=0;i<3;++i) 
     if(o_gp_flag[i]) 
      printf("a group of %d capital letter O's have been found togeth\n", i+1); 
} 
1

爲了避免兩個地打印的消息,一個爲OS在字符串中間和結束時額外的檢查對於OS ,我會建議一個如下的解決方案,其中一個內部循環消耗一個字符序列,直到非O或EOF消耗完,並且如果前面的Os序列非空,則外部循環打印一條消息:

#include <stdio.h> 
int main() { 
    int c, count; 
    do { 
     for (count=0; (c = getchar()) == 'O'; count++) {} 
     if (count) 
      printf("a group of %d O's found.\n", count); 
    } while (c != EOF); 
    return 0; 
} 

這裏是一個字符串指針代替的getchar(下同):

void test(char *p) { 
    int c, count; 
    do { 
     for (count=0; (c = *(p++)) == 'O'; count++) {} 
     if (count) 
      printf("a group of %d O's found.\n", count); 
    } while (c != 0); 
}