Thread: can anyone help to spot mistake in the code

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    5

    can anyone help to spot mistake in the code

    Hi!

    Can anyone help with a code I spend a week and cannot do it! I dunno why it does not work...
    Code:
    #include <stdio.h>
    #include <string.h>
    #define SIZE 100
    int strip()
    {
            char s[100];
            printf ("Enter some string \n");
            //printf ("1\n");
    
    
            fgets( s, 90, stdin);
            //printf ("2\n");
            int size;
            size=strlen(s);
            //size=sizeof(s)/sizeof(char);
            //printf ("3\n");
            int i=0;
            for (i; i<size; i++)
            {
                    if(s[i]!=' '|| s[i]!='\t')
                    {
                            printf ("Why I am not working????\n");
                            printf ("%c", s[i]);
                    }
            }
    return 0;
    }
    int main()
    {
            //char *s[100];
            //printf ("Enter some string: \n");
            //scanf ("%s",s);
            strip();
            return 0;
    }
    when I am writting something like: "Good morning" it comes like "Good morning" however I wanted "Goodmorning" I guess there is smth wrong with
    Code:
     if(s[i]!=' '|| s[i]!='\t')
    but I cannot understand what any help? Please Thanks, Sam
    Last edited by chess_queen; 10-21-2012 at 10:37 AM.

  2. #2
    Registered User
    Join Date
    Aug 2011
    Posts
    91
    Code:
    #include <stdio.h>
    #include <string.h>
    #define SIZE 100
    int strip()
    {
            char s[100];
            printf ("Enter some string \n");
            //printf ("1\n");
    
    
            fgets( s, 90, stdin);
            //printf ("2\n");
            int size;
            size=strlen(s);
            //size=sizeof(s)/sizeof(char);
            //printf ("3\n");
            int i=0;
            for (i; i<size; i++)
            {
                    if(s[i]!=' '&& s[i]!='\t')    /*<------------ heres the mistake*/
                    {
                                                    printf ("%c", s[i]);
                    }
            }
    return 0;
    }
    int main()
    {
            //char *s[100];
            //printf ("Enter some string: \n");
            //scanf ("%s",s);
            strip();
            return 0;
    }
    everything is right except the small mistake pointed above..it should be && and not ||..because if you use '||' the if block is entered if one condition is true..if you enter a space, it is not a tab..so the condition becomes true..if you enter a tab it is not a space.. so the condition again becomes true..
    it should not be a tab and it should not be a space also..
    Last edited by theju112; 10-21-2012 at 10:41 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can you spot the error in my code?
    By sigur47 in forum C Programming
    Replies: 4
    Last Post: 12-14-2011, 07:16 PM
  2. where is my mistake following this code..
    By transgalactic2 in forum C Programming
    Replies: 9
    Last Post: 10-18-2008, 01:44 PM
  3. Can you spot my mistake?
    By Brewer in forum C Programming
    Replies: 13
    Last Post: 11-12-2006, 12:50 PM
  4. Can anyone spot the error in my code
    By korbitz in forum C Programming
    Replies: 2
    Last Post: 01-05-2002, 02:05 AM
  5. returning to a spot in the code
    By Shadow in forum C Programming
    Replies: 4
    Last Post: 09-22-2001, 05:24 AM