Thread: counting words in a text file...

  1. #1
    Registered User
    Join Date
    Sep 2003

    Replacing string from a line

    Hello people,
    This is an amazing site for people who are interested in the c language andother wonderful languages too.

    Please i have just started learning about the c language, being a novice i stumbled on this piece of work, and i was wondering if you great people can help me.I'm working on a unix system.

    The program might be run as follows:

    Command line with redirection

    a.out "old" "new" <infile >outfile

    Note that input is from stdin and output to stdout.(keyboard to screen)
    The output should be the same as the input except that all
    occurences of the string "old" are replaced by "new".

    For example, if you run a.out lara D with input
    I love lara
    lara wakes me up
    then the ouput would be
    I love D
    D wakes me up

    Your program should work for any argument strings including
    those that contain spaces. However, newline characters are
    not allowed in the "old" or "new" strings. If they are found
    print an appropriate usage message to stderr.

    I'll appreciate any help sirs/ma's.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Waterloo, Texas
    If you're using the stl, std::string has a replace() member function. Barring that you have to work out the algorithm yourself.

    Happy coding!
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
        return std::pow
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Originally posted by Sebastiani
    If you're using the stl, std::string has a replace() member function. Barring that you have to work out the algorithm yourself.

    Happy coding!
    Nice thought, wrong board. C doesn't have classes.

    Anyway, as suggested, work out the problem. Write yourself some pseudocode. Write out how you'd do the problem in words first and then turn it into code.

    For example, were I trying to find the number 5 in a file, my pseudocode would be something like:
    open the file
    while i'm not done with the file
        read a number
        check to see if it's five
           if so, do whatever
           if not, keep looking
    close the file
    Something to that effect. It's a handy way to break down the problem to get you started.

    Hope is the first step on the road to disappointment.

  4. #4
    Registered User Markallen85's Avatar
    Join Date
    Nov 2002
    the general method for a search& replace that I would use is something like: (written in a mix of code and text)

    read "old" & "new" strings
    create temp buffer of size strlen(old)+1
    read in first strlen(old) characters to buffer
    while (c=getc(input)!=EOF)
      if (strcmp(buffer,old)==0)
        print new to output
        clear buffer
        load the next strlen(old) chars
        write first char of buffer to output
        shuffle all chars of buffer down one
        read in next char from input to last char of buffer
    we now have the whole of output printed, with the changes, except for the last (strlen(old)) characters, which are in the buffer.
    Check if buffer == old, if so write new, otherwise just print buffer to output and close.
    hope that's roughly intelligible

    "never argue with an idiot, they will drag you down to their level and beat you with experience"

  5. #5
    Registered User
    Join Date
    Sep 2003

    Thumbs up About My Problem

    Thanks to al lof you for the beautiful and intelligent insights. I'll work on your suggestions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 03:17 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM