Thread: Closing Program

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Closing Program


    Hello guys.

    I am havig trouble exiting this program. Basically, after I type EOF, the program keeps running. Any help would be appreciated.

    PS. Basically I enter two strings and I have to determine if the first one is a subsequence of the second.

    Code:
    #include <stdio.h>
    
    int main()
    {
        char s[1000],t[10000];
        int i,j;
        while(scanf("%s %s", s,t)&&s[0]!='E'&&s[1]!='O'&&s[2]!='F')
        {
                        for(i=0,j=0;(s[i]!='\0')&&(t[j]!='\0');j++)
                        {
                                                if(s[i]==t[j])
                                                {
                                                              i++;
                                                }
                        }
                        if(s[i]=='\0')
                        {
                                      puts("YES");
                        }
                        else
                        {
                                      puts("NO");
                        }
        }                       
        return 0;
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    59
    Rep Power
    3
    I ran the program. When I entered E, it ended, but not until I entered another string on the second line or after whitespace. That is because the scanf pattern "%s %s" has to match two different character strings separated by whitespace before it will complete and transfer control to the next line of code.
    Last edited by EEmaestro; November 14th, 2012 at 10:52 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0
    Would you recommend that I input both with gets("both strings read as one string with space in between") and then separate each one into two strings or would there be an easier way to go about it?
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,376
    Rep Power
    1871
    If by gets(), you mean fgets(), yes.
    Code:
    char buff[100];
    fgets(buff,sizeof(buff),stdin);
    You should never use gets, it is unsafe
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo