0

I am trying to write a function to scan in a string and find sets of the capital letter 'O'. Say my string is yyyOOOyyyOOyyyOyyy, it should print:

a group of 3 capital letter O's have been found together.

a group of 2 capital letter O's have been found together.

a group of 1 capital letter O's have been found together.

I cannot find a good way to do this. I'm trying to use nested loops, but I just don't have enough experience with it yet. Here is what I have come up with so far (I know it is completely non-functional). My code is also not allowing a user input using scanf (I have to use scanf for the assignment) Any help in correcting my loops and getting scanf to work would be great! Thanks!

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$
            }
}
lchristina26
  • 205
  • 5
  • 20
  • you're just counting the number of `O` chars. You need to keep track of current/previous chars to detect sequences. – Marc B Feb 15 '14 at 01:24

6 Answers6

1

In order to avoid having two places to print the message, one for Os in the middle of the string and an extra check for Os at the end, I'd suggest a solution like the following, where an inner loop consumes a sequences of characters until the a non-O or the EOF is consumed, and the outer loop prints a message if the leading sequence of Os was non-empty:

#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;
}

Here is the same with a string pointer instead of 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);
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
CliffordVienna
  • 7,995
  • 1
  • 37
  • 57
0

You might need to do something like this:

#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;
}

Notice that you can change what you want to search for by changing cTS variable.

Ghasan غسان
  • 5,577
  • 4
  • 33
  • 44
0

Try this:

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);
}
Timothy Gu
  • 3,727
  • 28
  • 35
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;
            }
        }
    }
}
kkaushi
  • 41
  • 4
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);
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
-1

I think if you put this in your loop, it will work.

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;
}
Nico
  • 6,395
  • 4
  • 25
  • 34
  • Does this handle `yyOOO` (a series of `O` at the end of the string)? – Jonathan Leffler Feb 15 '14 at 02:17
  • I did; it ignores the group of O's at the end. Did you try it? – Jonathan Leffler Feb 16 '14 at 02:58
  • @JonathanLeffler Nope, I didn't try it. This is our problem, not mine ;-) This is to inspire you to a solution. Anyway, you have enough solution in this thread already for such an easy problem so stop wasting our time by posting such comments. – Nico Feb 17 '14 at 14:38