[HELP] Check if "Just do it" is in users input

This is a discussion on [HELP] Check if "Just do it" is in users input within the C Programming forums, part of the General Programming Boards category; Make and run a program that will accept a user input of not more than 50 characters and find set ...

  1. #1
    Registered User
    Join Date
    Aug 2012
    Location
    Caloocan, Metro Manila, Philippines
    Posts
    17

    Unhappy [HELP] Check if "Just do it" is in users input

    Make and run a program that will accept a user input of not more than 50 characters and find set of letters that will produce the string “just do it” consecutively. The program should output the string “just do it” or “string not found” if the entered words were unable to produce the desired string.

    The problem in my work is if the letters in "JUST DO IT" is found even if it is not consecutive or it counted more than one of the same letter, it still says that it found "Just do it".

    This is what I have so far!
    Code:
    #include <ctype.h>
    #include <stdio.h>
    #include <string.h>
    
    int
    main(void)
    {
        char word[51];
        const char just_do_it[] = "JUST DO IT";
        int correctness = 0;
    
    
        gets(word);
    
    
        for (int i = 0; i < 40; i++)
        {
            word[i] = toupper(word[i]);
            for (int j = 0; j < 10; j++)
            {
                if (word[i] == just_do_it[j])
                    correctness++;
            }
        }
    
    
        if (correctness == 10)
            printf("\nJust do it.\n");
        else
            printf("\nString not found\n");
        return 0;
    }
    Last edited by Arbyn Acosta; 09-13-2012 at 06:25 AM.

  2. #2
    Registered User
    Join Date
    Aug 2012
    Posts
    41
    Hey, I debugged your code, there are some flaws in your code. This code is working fine. You can check the differences.

    Code:
    #include <ctype.h>
    #include <stdio.h>
    #include <string.h>int
    main(void)
    {
        char word[51];
        const char just_do_it[] = "just do it";
        int correctness = 0;
    
        gets(word);
        int len;
        len = strlen(word);   <<<<<<--------to get the length of string
        int i, j;
        for (i = 0; i < len; i++)
        {
            for (j = 0; j < 10; j++)
            {
                if (word[i+j] == just_do_it[j])
                    {
                    correctness++;
                    if(correctness == 10)
                            {
                            printf("\nJust do it.\n");
                            return 0;
                            }
                    }
               else
                    {
                    correctness=0;
                    break;
                    }
            }
        }
            printf("\nString not found\n");
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    When you run across a task like this, ask yourself "is this a common task?". Because if it is, then there's a good chance that it will already be part of the standard C headers:

    strstr() - searches through a string, returning either the base address of sub-string (it's first letter), or NULL.

    Part of string.h. That reduces your program to two lines of code: the strstr() line, and the subsequent if statement to see if the sub-string was found. In C, that is commonly reduced to one line of code.

    But you had fun and learned a lot I'm sure, writing your program. I liked your logic in it.

  4. #4
    Registered User
    Join Date
    Aug 2012
    Location
    Caloocan, Metro Manila, Philippines
    Posts
    17
    Wait. I liked the idea of sana.iitkgp but I want to ask further readers of what your understanding to this part of the instruction:

    "find set of letters that will produce the string “just do it” consecutively."

    Does it mean it has to find the letters consecutively even with interference for example "shut up and just do it" returns true while "do just it" returns false...

    or even with letters between them and the space is not counted
    "the quick brown fox jumps over the lazy dog" contains all the letters so its true!

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Am I missing something or is this:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main (void)
    {
        char input[128];
    
        fgets (input, sizeof(input), stdin);
    
        if (strstr(input, "JUST DO IT") != NULL)
            printf("Found string\n");
        else
            printf("Didn't find string\n"); 
    }
    a viable (and safer) replacement for your code?

  6. #6
    Registered User
    Join Date
    Aug 2012
    Posts
    41
    I misunderstood the question.

    This should solve your problem.

    Code:
    #include <ctype.h>
    #include <stdio.h>
    #include <string.h>
    int
    main(void)
    {
        char word[51];
        const char just_do_it[] = "justdoit";
        int correctness = 0;
    
        gets(word);
        int len;
        len = strlen(word);
        int i, j=0;
        for (i = 0; i < len; i++)
        {
            if (word[i] == just_do_it[j])
                    {
                    correctness++;
                    j++;
                    if(correctness == 8)
                            {
                            printf("\nJust do it.\n");
                            return 0;
                            }
                    }
        }
            printf("\nString not found\n");
        return 0;
    }

  7. #7
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    You cannot compare the contents of an array with ==.

    You need to either compare each element by itself or use a library function to do it for you (hint: lookup strcmp() for strings or memcmp() for general arrays).

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Arbyn Acosta View Post
    Wait. I liked the idea of sana.iitkgp but I want to ask further readers of what your understanding to this part of the instruction:

    "find set of letters that will produce the string “just do it” consecutively."

    Does it mean it has to find the letters consecutively even with interference for example "shut up and just do it" returns true while "do just it" returns false...

    or even with letters between them and the space is not counted
    "the quick brown fox jumps over the lazy dog" contains all the letters so its true!
    The target string "just do it", must be *exact*. "do just it", would be false, on the search.

    @memcpy: strstr() is all that's needed, and clearly a better alternative.

  9. #9
    Registered User
    Join Date
    Aug 2012
    Location
    Caloocan, Metro Manila, Philippines
    Posts
    17
    My personal interpretation of the instruction is that the letters should be found consecutively but not necessarily together.

    J... U... S... T... D... O... I... T...

    Then it would display "Just do it" because the letters were found consecutively.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Arbyn Acosta View Post
    My personal interpretation of the instruction is that the letters should be found consecutively but not necessarily together.

    J... U... S... T... D... O... I... T...

    Then it would display "Just do it" because the letters were found consecutively.
    Holy Shades of Bill Clinton! It depends what your definition of "is", is!! With a "consecutive" definition of "one after the other, without interruption", I would normally think "no", but this is an educational rather than a practical assignment, so I think "yes" - because it eliminates the easy answer of using strstr(), and forces you to work out the logic you have in your program.

    It's that work out of the logic, that makes this a really good exercise. So I believe you're right - and withdraw my suggestion of using strstr().

  11. #11
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    I vote for strstr. This might be an easy exercise for most of us, but we don't know at what level the OP is, and it's clear that it's easy to trip up on requirements based on the meaning of a single word.

    Here's my rationale for using strstr:
    con·sec·u·tive..Adjective
    * Following continuously.
    * In unbroken or logical sequence.
    Only strstr or a functionally equivalent algorithm satisfies this definition. I don't believe the assignment asks to find each letter in order with possibly intervening characters.

  12. #12
    Registered User
    Join Date
    Aug 2012
    Location
    Caloocan, Metro Manila, Philippines
    Posts
    17
    I think Abak is right. We should create a program that doesn't completely depend on a standard function because this is an educational assignment which forces the students to create their own way to solve the given problems. And the instruction also said "letters" so space are not counted!

  13. #13
    Registered User
    Join Date
    Aug 2012
    Location
    Caloocan, Metro Manila, Philippines
    Posts
    17

    Lightbulb

    Apparently my professor's PDF is not fully correct. The real instruction is the the letters J,U,S,T,D,O,I,T is found in the users input string without having them in order. So here's my revised code. My last problem is that when the program sees a letter T it replaces both the 1st and last T's in the just_do_it array with space.

    Code:
    #include <ctype.h>
    #include <stdio.h>
    #include <string.h>
    
    
    int
    main(void)
    {
        char string[51], just_do_it[] = "JUSTDOIT";
    
    
        fgets(string, 51, stdin);
    
    
        for (int i = 0, end = strlen(string); i < end; i++)
            string[i] = toupper(string[i]);
    
    
        for (int i = 0, end = strlen(string), correct = 0; i < end; i++)
        {
            for (int j = 0; j < 8; j++)
            {
                if (string[i] == just_do_it[j] && just_do_it[j] != ' ' && string[i] != ' ')
                {
                    correct++;
                    just_do_it[j] = ' ';
    
    
                    if (correct == 8)
                    {
                        printf("\nJust do it\n");
                        return 0;
                    }
                }
            }
        }
    
    
        printf("\nString not found\n");
        return 0;
    }
    What I need is two T's instead of only one T

    So:
    Justdoi = FALSE
    doTitsJU = TRUE

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Quote Originally Posted by Arbyn Acosta
    The real instruction is the the letters J,U,S,T,D,O,I,T is found in the users input string without having them in order.
    I suspect that this will become easier if you sort the input string (and maybe the letters too) first.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You need to match up 9 letters, instead of 8. So add another T to the target string, and run it pretty much as before, testing the input, letter by letter. In your logic, if you find one T, be sure to keep the second T as a letter you also need to find, in the target string. So it wouldn't matter if the prof decided he wanted the target string to even add more letters, with more repetition of letters.

    You could sort the input, and the target letters, but I wouldn't bother with it, unless the prof says the run time is critical.

    There's more than one way to do this.

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

Similar Threads

  1. Replies: 12
    Last Post: 07-05-2012, 09:32 AM
  2. nbin=fopen("input.txt","a"); doesn't work?
    By Adam Rinkleff in forum C Programming
    Replies: 2
    Last Post: 06-23-2011, 03:57 PM
  3. Retrieving local name of "Users" group
    By Magos in forum C# Programming
    Replies: 1
    Last Post: 09-14-2007, 03:16 PM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

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