Thread: Help remove spaces + punctuation

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    1

    Question Help remove spaces + punctuation

    Hi
    can anyone teach me how to remove spaces + punctuation in a String?
    eg.
    String word = "Help me! Help me!";
    after removespaces+punctuation

    print:
    HelpmeHelpme

    Thank you very much!

  2. #2
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    check each character in the string... if it's one you want to delete, then delete it and shift all the other remaining characters back one...
    hasafraggin shizigishin oppashigger...

  3. #3
    "The Oldest Member Here" Xterria's Avatar
    Join Date
    Sep 2001
    Location
    Buffalo, NY
    Posts
    1,039
    Code:
    for (int x=0; x<strlen(word);x++)
    {
         if(word[x] = ' ')
    {
         delete word[x];
    }
    else if(word[x] = '!')
    {
    delete word[x];
    }
    }
    Somthing like that

  4. #4
    Unregistered
    Guest
    look into standard function strtok()...do a search on this board....

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Xterria. You do not use delete like that mate. That is a recipe for disaster.To do this look into the functions isalpha,isspace and ispunct. This will allow you to identify which chars should stay and which should go. Do not do what xterria tried to show you.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    4

    Thumbs up Same Issue

    I am having the same issue. I need to find a palindrome, but I can't get my stuff to shift over after I call the space's and puntuation Null.

    Basically, I sitll need to find a way to reverse the string and do a strcmp(original strings with all lower-space and then removed spaces and punctuation, new reversed string with all punctuation/spaces removed, and lower spaced).

    I really need help on the part where I do remove all the spaces/punctuation.
    Right now in function removeSpaces, I look to see if it is basically not a lower case letter or a number, and if it isn't it does _____. I need the _____ to remove and shift the string the the left... any help would be GREATLY appreciated.

    And any tips on the string reversal. I heard about strrev.... does that work in C programming?

    Thanks a ton!

    Also I don't know how to post the code straight to here so.... Im sorry!
    Last edited by Computergeek931; 11-17-2010 at 07:44 PM.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    4
    Okay I figured out how to post code. But yea, heres what it is.
    Takes in "input.txt" as a string

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    #define N 45
    #define LENGTH 80
    
    int readline(FILE* f, char *line);
    void swap(char *string1, char *string2);
    void sort(char name[N][LENGTH]);
    void lower(char name[N][LENGTH]);
    void removeSpace(char name[N][LENGTH]);
    
    int main(void)
    {
            FILE *inp;
            int i,j;  
            char name[N][LENGTH];
            int countLine = 0;   
            int len = 0;
            
    	inp = fopen("input.txt","r");
    
    	for(i = 0; i < N; i++){
                    len = readline (inp, name[i]);
                    if( len == 0 )
                            break;
            }
    	countLine = i;
            lower(name);
    	sort(name);
    	removeSpace(name);
    	for(i = 0; i < N-1; i++){
    		printf("%s\n",name[i]);
    	}
    }        
    void lower(char name[N][LENGTH])
    {
    	int i,j;
    	for(i = 0; i < N-1; i++){
    		for(j = 0; j < strlen(name[i]); j++){
    			name[i][j]=tolower(name[i][j]);
    		}	
    	}
    }
    void removeSpace(char name[N][LENGTH])
    {
    	int i,j;
    	for(i = 0; i < N-1; i++){
    		for(j = 0; j < strlen(name[i]); j++){
    			if((name[i][j] < '0') || (name[i][j] < 'a' && name[i][j] > '9') || name[i][j] > 'z') {
    				name[i][j] = '\0';
    				strcat(name[i],&name[i][j]);
    			}	
    		}
    	}
    }
    void sort(char name[N][LENGTH])
    {
            int i;
            int j;
            for(i = 0; i < N-1; i++){
                    for(j = i+1; j < N; j++){
    			if(strcmp(name[i], name[j])>0)
                                            swap(name[i], name[j]);
                    }
            }
    }
    int readline(FILE* f, char *line)
    {
            int len = 0;
            char c;
            while ( (c = fgetc(f) ) != EOF && c != '\n')
            {
                    line[len++] = c;
                    line[len] = '\0';
            }
            return len;
    }        
    void swap(char *string1, char *string2)
    {
            char t[LENGTH];
            strcpy(t, string1);
            strcpy(string1, string2);
            strcpy(string2, t);
    }

  8. #8
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    You could simplify it to a function that replaces all punctuation with a space, and a function that removes spaces.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Posts
    4
    Well either way, I need to figure out how to remove the spaces...
    any help on that?

  10. #10
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    This looks like it would be better off in the C forum though from your initial post it looks like you were trying to use a std::string container for your "word"s, so... are you looking for C or C++ assistance?



    #1.
    Code:
    int readline(FILE* f, char *line)
    {
        int len = 0;
        char c;
        while ( (c = fgetc(f) ) != EOF && c != '\n')
    The fgetc function returns an int, not char.



    #2.
    Code:
    int main(void)
    {
        ...
    
        for(i = 0; i < N; i++){
            len = readline (inp, name[i]);
            if( len == 0 )
                break;
        }
    
        ...
    
        for(i = 0; i < N-1; i++){
            printf("%s\n",name[i]);
        }
    }
    
    void lower(char name[N][LENGTH])
    {
        int i,j;
        for(i = 0; i < N-1; i++){
            ...
    }
    
    void removeSpace(char name[N][LENGTH])
    {
        int i,j;
        for(i = 0; i < N-1; i++){
        ...
    }
    I believe you've got some "off by one" errors there in red.

    #3. It's possible to not read the full N strings from a file but all your code would assume N string were read and you would therefore attempt to print/sort/etc N strings that potentially contained garbage.



    If you were looking at a C++ solution using a std::string container instead I'd suggest looking at the isspace and ispunct functions along with the STL std::remove_if function. Then the removal of both punctuation and spaces could each be a one-line thing.

    Reversal of the string is also a one-liner using reverse iterators to initialize a new std::string object using the original. ...and lowering the case is another one-liner using the STL std::transform function along with tolower.
    Last edited by hk_mp5kpdw; 11-18-2010 at 06:59 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  11. #11
    Registered User
    Join Date
    Nov 2010
    Posts
    4
    Thanks. The functions work just fine though.

    I just need to figure out how to take the whole string and shift it left.

    right now it out puts

    'to' instead of 'to take out the garbage' becasue when it reads a space, the /0 character indicates the end of the strgin.

    isnt there like...a delete key for C?lol

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Changing 3 spaces to tabs
    By dnguyen1022 in forum C Programming
    Replies: 2
    Last Post: 12-22-2008, 12:51 AM
  2. Tabs or Spaces
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 46
    Last Post: 04-08-2007, 11:45 AM
  3. How to remove end spaces from string
    By nitinmhetre in forum C Programming
    Replies: 4
    Last Post: 12-16-2006, 01:14 AM
  4. Remove Spaces?
    By 98dodgeneondohc in forum C Programming
    Replies: 15
    Last Post: 04-21-2005, 10:16 AM
  5. Replies: 5
    Last Post: 06-30-2003, 12:52 PM