Thread: Help me with a space-deleting program

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    7

    Angry Help me with a space-deleting program

    Hello! I need to write a program that reads char by char, deletes spaces and hyphenation in front of dots and commas in text file. Then I need to put the edited text in another file. I wrote a program that removes a single space/hyphen, but how do I remove several of them? The use of string.h and other libraries except standard lib is forbidden!

    Help a noob, I need this for tomorrow!
    Code:
    #include <stdio.h>int main(int argc, char *argv[]) 
    {
    
    
        FILE *f1;
        FILE *f2;
        char ch1;
        char ch2;
        
        if(argc==1) 
        {
            printf("File is unknown\n");
            return 1;
        }
        f1=fopen(argv[1], "r");
        if(!f1) 
        {
            printf("Error opening %s\n", argv[1]);
            return 2;
        }
        f2=fopen(argv[2], "w");
        if(!f2) 
        {
            printf("Error creating %s\n", argv[2]);
            return 3;
        }
        while((ch1=getc(f1))!=EOF)
        {   
            if((ch1==' ')||(ch1=='\n')) 
            {
    
    
                if((ch2 = getc(f1))=='.')
                {
                    fputc('.', f2);
                }
        
                else 
                {
                    fputc(ch1, f2);
                    fputc(ch2, f2);
                }
            } 
            else fputc(ch1, f2);
        }
    printf("The editing is complete!");    
    return 0;
    }
    The code should remain analtered higher than first while loop.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    The procedure it comes to my mind is this.
    Let f1 be the input file and f2 the output one.
    Read a character from f1. Check if it is legal (in other words, check if it is not a comma, space or the other thing you say (seems to be a Greek word and I am Greek, but yet I do not know it :P)).
    If it is legal, then put it in f2.
    Else, continue to the next character from f1.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    7
    Quote Originally Posted by std10093 View Post
    The procedure it comes to my mind is this.
    Let f1 be the input file and f2 the output one.
    Read a character from f1. Check if it is legal (in other words, check if it is not a comma, space or the other thing you say (seems to be a Greek word and I am Greek, but yet I do not know it :P)).
    If it is legal, then put it in f2.
    Else, continue to the next character from f1.
    Looks kinda like that, but I need to remove a space or a hyphen that goes before a dot/comma. I need to preserve the punctuation marks and delete the spaces before them.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I love it! He's Greek, and the word is "Greek" to him. LOL!

    The answer is, instead of using an if statement to test and then remove if appropriate, one space - use a while loop, with the same test to remove as many as you want.

    So you will have a while loop, inside your current while loop. You could also have a variable to "remember" what the last char was, and if it was a repeating space char, then delete it. No nested while loop needed for that kind of logic.
    Last edited by Adak; 01-10-2013 at 11:51 AM.

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    7
    Well, I thought about that. And tried to try (rofl) this. But if some pro would show this done properly to me, it'll be great, 'cause I fail so hard.

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by Adak View Post
    I love it! He's Greek, and the word is "Greek" to him. LOL!
    I google it.
    Code:
    The term derives from Ancient Greek ὑφ᾽ ἕν (hyph’ hén), contracted from ὑπό ἕν (hypó hén) "in one" (literally "under one").[1]
    
    The term ἡ ὑφέν (hyphén) was used for a caret-like (^) sign written below two consecutive letters 
    to indicate that they belong to the same word (where it was necessary to avoid ambiguities in times before the space was used regularly).
    It is not clear to any modern Greek I guess :P
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by n00b5ter View Post
    Well, I thought about that. And tried to try (rofl) this. But if some pro would show this done properly to me, it'll be great, 'cause I fail so hard.
    I am not a pro, but I can take a look at your code, if you want
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by n00b5ter View Post
    Hello! I need to write a program that reads char by char, deletes spaces and hyphenation in front of dots and commas in text file.
    What about spaces/hyphens which aren't before a dot or a comma?

    As I understand it you want to keep them, right?

    Bye, Andreas

  9. #9
    Registered User
    Join Date
    Jan 2013
    Posts
    7
    Quote Originally Posted by AndiPersti View Post
    What about spaces/hyphens which aren't before a dot or a comma?

    As I understand it you want to keep them, right?

    Bye, Andreas
    Of course, I want.

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    @std10093 - I'm pretty sure this is what Adak was referring to (leading me to much laughter).

  11. #11
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by n00b5ter View Post
    Of course, I want.
    Good
    Quote Originally Posted by Matticus View Post
    @std10093 - I'm pretty sure this is what Adak was referring to (leading me to much laughter).
    I learned that phrase in this forum, when I had posted where Pi (π = 3.14) got its name. Yeah, it make me smile too
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  12. #12
    Registered User
    Join Date
    Jan 2013
    Posts
    7
    Well, this is a quite laughable thread.
    But what about my problem? I'm so tired and don't know how to solve it. Any code examples, please?

  13. #13
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Oh, I was waiting for you to saw the code

    Yes, sorry for that, but it was really funny

    Actually, I have to go out, but sure somebody else will check your code
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  14. #14
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Understand that it's difficult to provide "sample" code that does the task, without just giving out the solution.

    If I understand your specifications correctly, your solution is really close. Two points:

    1. If you're not copying spaces or hyphens before certain characters, why are you checking for spaces and newlines? That's an easy fix.

    2. If you want to check the next character for periods or commas, why are you only checking for periods? This is another easy fix.

    Let me know if this helps you correct your current code.

  15. #15
    Registered User
    Join Date
    Jan 2013
    Posts
    7
    Quote Originally Posted by Matticus View Post
    Understand that it's difficult to provide "sample" code that does the task, without just giving out the solution.

    If I understand your specifications correctly, your solution is really close. Two points:

    1. If you're not copying spaces or hyphens before certain characters, why are you checking for spaces and newlines? That's an easy fix.

    2. If you want to check the next character for periods or commas, why are you only checking for periods? This is another easy fix.

    Let me know if this helps you correct your current code.
    1.Oh yeah, I actually meant newlines, not hyphens. These two things are called a bit close in Russian.
    2. I feel clumsy with that, everything I try fails.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Deleting a program
    By 182 in forum C++ Programming
    Replies: 5
    Last Post: 02-23-2006, 09:10 PM
  2. a space in a program path
    By eam in forum C Programming
    Replies: 2
    Last Post: 10-26-2003, 10:19 PM
  3. Deleting a file while in a program
    By Blizzarddog in forum C++ Programming
    Replies: 1
    Last Post: 07-17-2003, 12:01 PM
  4. deleting a space from a string
    By niroopan in forum C++ Programming
    Replies: 4
    Last Post: 01-10-2003, 09:43 PM
  5. Disappointed in the Space Program
    By Cheeze-It in forum A Brief History of Cprogramming.com
    Replies: 27
    Last Post: 11-26-2002, 06:26 AM

Tags for this Thread