Thread: removing punktuation from a string with while loop

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    2

    removing punctuation from a string with while loop

    Hi there,

    this code should remove spaces and commas from the string , but I am wondering why the if condition fails to detect the comma and spaces .

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>
    
    int main(int argc, char **argv)
    {
        char txt[100];
        int i = 0;
        strcpy(txt,"the postman, barely made it to the front door.");
        
        while(txt[i] != '.')
        {
            if(txt[i] != ','|| txt[i] != ' '){
            printf("%c",txt[i]);
        }
        i++;
        }
        return 0;
    }
    Result:
    the postman, barely made it to the front door

    but the output must be like this:
    thepostmanbarelymadeittothefrontdoor
    but when I try to remove just the comma or spaces it works fine. It must be somthing with the multiple condition in the if statment.
    Last edited by marthinker; 05-13-2012 at 08:42 AM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,547
    If the character is not equal to a comma you print the character, a space is not a comma. You will need to change your logic. Try something like:
    Code:
    if(!(txt[i] == ',' || txt[i] == ' '))
    Jim

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    It seems that the most straight forward thing you can do is to use a && instead of ||, because you want both conditions to be true before you print.

    Code:
    if(txt[i] != ',' && txt[i] != ' ')

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Read the "if" statement out loud: "if the character is not a comma or if the character is not a space".

    A space is not a comma, and a comma is not a space, hence, it always works.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    2
    it's working now, thanks guys for your help.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I guess the closest you can get to natural spoken language is

    Code:
    if( ! ( txt[i] == ',' ||  txt[i] == ' ' ) )
    Kurt

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    "If the character isn't a comma and the character isn't a space, print the character."

  8. #8
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I noticed just after posting that it is possibly only in my language/dialect where you would say it this way.
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Removing Items from a list - for loop
    By kerrymaid in forum C Programming
    Replies: 2
    Last Post: 01-19-2011, 04:58 PM
  2. Removing char from string
    By cruxxe in forum C Programming
    Replies: 5
    Last Post: 05-22-2002, 03:59 PM
  3. Removing junk from end of string
    By dirkduck in forum C++ Programming
    Replies: 7
    Last Post: 05-20-2002, 01:15 PM
  4. Removing '\n' From a String
    By Mace in forum C Programming
    Replies: 4
    Last Post: 12-02-2001, 07:42 AM
  5. Removing 'strcat' ed string.
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 11-14-2001, 12:09 AM