counting problem

This is a discussion on counting problem within the C Programming forums, part of the General Programming Boards category; techies, i don't where to start to countr two characters in any patterns or from any words like "ei" from ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    9

    counting problem

    techies,

    i don't where to start to countr two characters in any patterns or from any words like "ei" from "Receive eieiten pooeesiec".

    can you give some hints?

  2. #2
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    strstr
    Don't quote me on that... ...seriously

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    > strstr
    strstr() in a loop.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    9
    i am thinking to use not strstr. suppose, i don't know about str. i just want to use one character at a time. how to remember previous character and current char?

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by javani View Post
    i am thinking to use not strstr. suppose, i don't know about str. i just want to use one character at a time. how to remember previous character and current char?
    Linked Lists!, with a 'next char node' and/or a 'previous char node'...

    Or a loop, with something like;
    Code:
    char * s = "Hello";
    while(*s)
    {
    	// note: you should be checking if p is going to be valid, but thats upto you.
    	// on the first iteration its not going to work... a simple 'if' statement or for() loop would fix this.
    	char p = *s-1, c = *s;        // p is prev, c is current char
    	*s++;
    }
    Last edited by zacs7; 03-31-2007 at 06:30 PM.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    If the OP doesn't know about strings, I highly doubt they know about pointers.

    Perhaps something like this:
    Code:
    char s[] = "Receive eieiten pooeesiec";
    size_t x, len = strlen(s);
    
    for(x = 1; x < len; x ++) {
        s[x];  /* char */
        s[x-1];  /* previous char */
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by dwks View Post
    If the OP doesn't know about strings, I highly doubt they know about pointers.

    Perhaps something like this:
    Code:
    char s[] = "Receive eieiten pooeesiec";
    size_t x, len = strlen(s);
    
    for(x = 1; x < len; x ++) {
        s[x];  /* char */
        s[x-1];  /* previous char */
    }
    Then how do you know they didn't "hover" over for loops too ?

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Yes, well, that's a risk I'll have to take. The more [solutions] the merrier.

    If you're just looking for either 'e' or 'i' followed by either an 'e' or an 'i', then I suggest using something like this:
    Code:
    char str[] = "Receive eieiten pooeesiec";
    char look[] = "ei";
    char *p;
    
    p = strpbrk(str, look);
    if(strchr(look, p[1])) {
        /* found, p[0] and p[1] */
    }
    You'll notice I provided links for the functions I used. Say, maybe I should get codeform to do that . . . [edit] codeform version 1.2.0 #2 [/edit]
    Last edited by dwks; 03-31-2007 at 07:24 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Good idea

  10. #10
    Registered User
    Join Date
    Mar 2007
    Posts
    9
    BUT, what if I haven't studied pointer, array and functions too. I've just finished studying loop and basic of C like getting characters or lines from key board, and counting them.

    Isn't there any way with much knowledge?

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You've managed to get three people to do your homework for you, or very close to it. Let's see what you actually have done.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Mar 2007
    Posts
    9
    Quote Originally Posted by quzah View Post
    You've managed to get three people to do your homework for you, or very close to it. Let's see what you actually have done.

    Quzah.
    sorry, u r interpreting wrongly. I am not a computer student nor it's a homework. I am just following book and learning C from that. And, what I am asking is WHAT I don't know, or don't know how to start?

    I've done word counting program, vowel counting. But it's out of my reach, i think. I don't know how to do two character comparision at a time. Hope cleared!

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use one variable to track the current character you've just read, and one to hold the one you read right before that. If they're not the same, move this one into the "last time" holder, and read a new one into the "just read" holder. Repeat until done.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    Mar 2007
    Posts
    9
    plz help me. it is stucking in second getchar().

    Code:
    while((ch=getchar())!='#')
     {
                  ch1 = ch;
                  ch2=getchar();
                  if(ch1=='a' && ch2=='b')
                            count++;
      }
      printf("pattern ab = &#37;d\n",count);

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    while( (ch=getchar()) != '# ) /* read until you find a # */
    {
        if( ch == first_char_to_find )
        {
            int ch2 = getchar( );
            if( ch2 == second_char_to_find )
                yay++;
            else
                ungetc( ch2, stdin );
        }
    }
    Something like that?


    Quzah.
    Hope is the first step on the road to disappointment.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM
  3. Segmentation Fault Problem: Urgent Help
    By bodydrop in forum C Programming
    Replies: 3
    Last Post: 05-05-2006, 09:02 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21