Really simple string functions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • juvenuts@hotmail.com

    #1

    Really simple string functions

    Hi, I'm a complete newbie to C, but I wanted to get started by writing
    a few simple programs that print out strings.

    1. The first thing I wanted to do was write a program that uses getchar
    to read one letter at a time, and uses printf to print it out as
    entered. I also wanted to make it terminate when the EOF character is
    entered.



    2. I also wanted to add to this by writing another program that prints
    out a string, ignoring everything that follows a specified character
    until a new line is found..

    e.g. if I wanted to ignore everything after the % character and the the
    input is

    This is a string of text % BLAHBLAHBLAH
    % BLAHBLAHBLAH
    This is a string of text

    Then the output will be

    This is a string of text

    This is a string of text



    3. I know these programs might be getting a bit repetitive for all you
    experienced C programmers, but I'd like to make a bunch of variations,
    the next one being this: I'm aware that there is an "ispunct" function,
    so I wanted to write a program that removes punctuation from input
    strings.



    4. Finally, how would I program simple substitutions for specified
    characters in a string? e.g. say I wanted to substitute every
    occurrence of "a" with "BANG!"

    e.g. "I am a newbie to C programming" would turn into
    "I BANG!m BANG! newbie to C progrBANG!mming "


    Any help would be very much appreciated, thanks in advance.

  • Michael Mair

    #2
    Re: Really simple string functions

    juvenuts@hotmai l.com schrieb:[color=blue]
    > Hi, I'm a complete newbie to C, but I wanted to get started by writing
    > a few simple programs that print out strings.
    >
    > 1. The first thing I wanted to do was write a program that uses getchar
    > to read one letter at a time, and uses printf to print it out as
    > entered. I also wanted to make it terminate when the EOF character is
    > entered.[/color]

    EOF is not a character, it is an int value indicating that the
    end of the stream has been encountered.
    This means that you need an int variable to store the return
    value of getchar().

    Show us your best shot at the program.

    [color=blue]
    > 2. I also wanted to add to this by writing another program that prints
    > out a string, ignoring everything that follows a specified character
    > until a new line is found..
    >
    > e.g. if I wanted to ignore everything after the % character and the the
    > input is[/color]
    <snip>

    One remark: Decide whether you want to work line-wise or whether
    you want to have one string potentially containing one or more '\n'
    characters.
    [color=blue]
    > 3. I know these programs might be getting a bit repetitive for all you
    > experienced C programmers, but I'd like to make a bunch of variations,
    > the next one being this: I'm aware that there is an "ispunct" function,
    > so I wanted to write a program that removes punctuation from input
    > strings.[/color]

    Note that ispunct() returns true for every non-alphanumeric, non-space
    character i.e. ispunct(c) returns !isalnum(c) && !isspace(c); this may
    not be what you want. In addition, this depends on the locale; in the
    "C" locale, 'ß' may lead to another result than in another locale.
    It may be better to make your own IsPunct() function which works
    with strchr(StringCo ntainingPunctua tionCharacters, c).

    The is... functions expect characters cast to unsigned char as
    arguments.
    [color=blue]
    > 4. Finally, how would I program simple substitutions for specified
    > characters in a string? e.g. say I wanted to substitute every
    > occurrence of "a" with "BANG!"
    >
    > e.g. "I am a newbie to C programming" would turn into
    > "I BANG!m BANG! newbie to C progrBANG!mming "[/color]

    Count number of occurrences of the character c to be replaced, N,
    malloc() storage which can store the longer string (longer by
    N*(strlen("BANG !")-1)) -- don't forget to allocate the byte for
    the string terminator '\0' --, then copy every non-c character over
    unchanged and insert "BANG!" whenever you encounter c.
    [color=blue]
    > Any help would be very much appreciated, thanks in advance.[/color]

    Show us your respective best shots at implementing the above;
    show us the complete programmes (if not too long).


    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.

    Comment

    • Richard Heathfield

      #3
      Re: Really simple string functions

      juvenuts@hotmai l.com said:
      [color=blue]
      > Hi, I'm a complete newbie to C, but I wanted to get started by writing
      > a few simple programs that print out strings.
      >
      > 1. The first thing I wanted to do was write a program that uses getchar
      > to read one letter at a time, and uses printf to print it out as
      > entered.[/color]

      Since your system almost certainly buffers standard input, this might prove
      a trickier task than you expected.
      [color=blue]
      > I also wanted to make it terminate when the EOF character is
      > entered.[/color]

      There is no such character. In C, EOF is a state, rather than a character.
      You can tell your system that you have no more data for this program, in
      some system-dependent way. When you do this, standard C library calls will
      return either EOF (which is defined to be some negative int value or other
      - the exact value depending on your implementation) or perhaps NULL,
      depending on the function.
      [color=blue]
      > 2. I also wanted to add to this by writing another program that prints
      > out a string, ignoring everything that follows a specified character
      > until a new line is found..[/color]

      That sounds easy enough. Some hints for you: a newline character is '\n',
      and you can store the current state of the program (ignoring or not
      ignoring) as an int, perhaps clearing it (i.e. making it 0) when you are
      not ignoring input, and setting it (i.e. making it 1) when you are ignoring
      input.

      [color=blue]
      > 3. I know these programs might be getting a bit repetitive for all you
      > experienced C programmers, but I'd like to make a bunch of variations,
      > the next one being this: I'm aware that there is an "ispunct" function,
      > so I wanted to write a program that removes punctuation from input
      > strings.[/color]

      Look up the 'if' construct.

      [color=blue]
      > 4. Finally, how would I program simple substitutions for specified
      > characters in a string? e.g. say I wanted to substitute every
      > occurrence of "a" with "BANG!"[/color]

      If you only wanted to copy an input stream to an output stream, filtering as
      you suggest, this is easy enough, but if you wish to perform substitutions
      in an actual string then you will need to learn about storage. Consider the
      following definition:

      char foo[] = "HELLO WORLD!";

      This reserves storage for an array of 13 characters, as follows:

      +---+---+---+---+---+---+---+---+---+---+---+---+---+
      | H | E | L | L | O | _ | W | O | R | L | D | ! |NUL|
      +---+---+---+---+---+---+---+---+---+---+---+---+---+
      |---|---|---|---|---|---|---|---|---|---|---|---|---|

      Now consider the problem of substituting R with AB. There are four parts to
      this problem. First, you have to find the text you plan to change. (That's
      step 1.) Then comes step 2: to retain the stuff before the text to be
      changed:

      +---+---+---+---+---+---+---+---+
      | H | E | L | L | O | _ | W | O |
      +---+---+---+---+---+---+---+---+
      |---|---|---|---|---|---|---|---|---|---|---|---|---|

      That bit's easy enough. Now you have to bolt on the new text:

      +---+---+---+---+---+---+---+---+---+---+
      | H | E | L | L | O | _ | W | O | A | B |
      +---+---+---+---+---+---+---+---+---+---+
      |---|---|---|---|---|---|---|---|---|---|---|---|---|

      That's easy too. But the fourth stage is to add on the text that comes
      /after/ the change:

      +---+---+---+---+---+---+---+---+---+---+---+---+---+---+
      | H | E | L | L | O | _ | W | O | A | B | L | D | ! |NUL|
      +---+---+---+---+---+---+---+---+---+---+---+---+---+---+
      |---|---|---|---|---|---|---|---|---|---|---|---|---|

      Oops. As the diagram shows, your data now exceeds the space available for
      it.

      A number of approaches can be used to solve this problem, but frankly if
      you're struggling with the ideas you mentioned earlier, this is a bit
      beyond your current powers. Give it time. Learn easier stuff first.


      --
      Richard Heathfield
      "Usenet is a strange place" - dmr 29/7/1999

      email: rjh at above domain (but drop the www, obviously)

      Comment

      • juvenuts@hotmail.com

        #4
        Re: Really simple string functions

        Thanks for the reply Michael.

        I'm still working through my projects one by one, so instead of trying
        to implement all the advice you've given in one go, I'll just try to
        get the first part working for now.

        [color=blue]
        > EOF is not a character, it is an int value indicating that the
        > end of the stream has been encountered.
        > This means that you need an int variable to store the return
        > value of getchar().[/color]

        Okay, this is what I've got so far...


        #include <stdio.h>

        int main (int argc, char** argv){

        char text <--- this will store the return value of getchar
        char letter
        letter = getchar();
        while (letter != EOF) {
        ...
        ... ()
        ...
        letter = getchar()
        }

        return 0;

        [color=blue]
        > One remark: Decide whether you want to work line-wise or whether
        > you want to have one string potentially containing one or more '\n'
        > characters.[/color]

        I'd like the program to be able to take a string containing one or more
        '\n' characters and carry out the task I've described.

        [color=blue]
        > Note that ispunct() returns true for every non-alphanumeric, non-space
        > character i.e. ispunct(c) returns !isalnum(c) && !isspace(c); this may
        > not be what you want. In addition, this depends on the locale; in the
        > "C" locale, 'ß' may lead to another result than in another locale.
        > It may be better to make your own IsPunct() function which works
        > with strchr(StringCo ntainingPunctua tionCharacters, c).[/color]


        Actually, the pre-defined ispunct() function will be sufficient for my
        needs, so I'd like to be able to write the program using it.

        Comment

        • Michael Mair

          #5
          Re: Really simple string functions

          juvenuts@hotmai l.com schrieb:[color=blue]
          > I'm still working through my projects one by one, so instead of trying
          > to implement all the advice you've given in one go, I'll just try to
          > get the first part working for now.
          >[/color]
          1) Context: Reading characters with getchar() and outputting them until
          EOF is encountered[color=blue][color=green]
          >>EOF is not a character, it is an int value indicating that the
          >>end of the stream has been encountered.
          >>This means that you need an int variable to store the return
          >>value of getchar().[/color][/color]

          Please quote sufficient context -- then everybody sees what is
          going on and can help you.
          [color=blue]
          > Okay, this is what I've got so far...
          >
          > #include <stdio.h>
          >
          > int main (int argc, char** argv){
          >
          > char text <--- this will store the return value of getchar
          > char letter[/color]
          int letter;
          as discussed above.[color=blue]
          > letter = getchar();
          > while (letter != EOF) {
          > ...
          > ... ()
          > ...
          > letter = getchar()
          > }
          >
          > return 0;[/color]

          Please copy and paste the real, compiling programme.


          2) Context: Implementing a line comment remover for % as comment
          character
          [color=blue][color=green]
          >>One remark: Decide whether you want to work line-wise or whether
          >>you want to have one string potentially containing one or more '\n'
          >>characters.[/color]
          >
          > I'd like the program to be able to take a string containing one or more
          > '\n' characters and carry out the task I've described.[/color]

          See Richard Heathfield's comments in his reply,
          <2oydnZ9vaIPrhL jZRVny1A@bt.com >

          Cheers
          Michael
          --
          E-Mail: Mine is an /at/ gmx /dot/ de address.

          Comment

          • pete

            #6
            Re: Really simple string functions

            juvenuts@hotmai l.com wrote:
            [color=blue]
            > Okay, this is what I've got so far...
            >
            > #include <stdio.h>
            >
            > int main (int argc, char** argv){
            >
            > char text <--- this will store the return value of getchar
            > char letter[/color]

            int letter; /* Seriously, it has to be int, not char */
            [color=blue]
            > letter = getchar();[/color]

            /* Otherwise, the (letter != EOF) expression, won't work right */
            [color=blue]
            > while (letter != EOF) {[/color]

            /*
            ** The type of EOF is int. The return type of getchar is also int.
            */

            --
            pete

            Comment

            • CBFalconer

              #7
              Re: Really simple string functions

              Michael Mair wrote:[color=blue]
              > juvenuts@hotmai l.com schrieb:
              >[/color]
              .... snip ...[color=blue]
              >[color=green]
              >> 3. I know these programs might be getting a bit repetitive for
              >> all you experienced C programmers, but I'd like to make a bunch
              >> of variations, the next one being this: I'm aware that there is
              >> an "ispunct" function, so I wanted to write a program that
              >> removes punctuation from input strings.[/color]
              >
              > Note that ispunct() returns true for every non-alphanumeric,
              > non-space character i.e. ispunct(c) returns !isalnum(c) &&
              > !isspace(c); this may not be what you want. In addition, this
              > depends on the locale; in the "C" locale, 'ß' may lead to another
              > result than in another locale. It may be better to make your own
              > IsPunct() function which works with
              > strchr(StringCo ntainingPunctua tionCharacters, c).
              >
              > The is... functions expect characters cast to unsigned char as
              > arguments.[/color]

              Which is what getchar() returns. So

              int ch;
              ....
              if (ispunct(ch = getchar())) {
              ....
              }
              else {
              ....
              }

              behaves as expected.


              --
              Some informative links:
              news:news.annou nce.newusers
              Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!






              Comment

              • santosh

                #8
                Re: Really simple string functions

                juvenuts@hotmai l.com wrote:[color=blue]
                > Hi, I'm a complete newbie to C, but I wanted to get started by writing
                > a few simple programs that print out strings.
                >
                > 1. The first thing I wanted to do was write a program that uses getchar
                > to read one letter at a time, and uses printf to print it out as
                > entered. I also wanted to make it terminate when the EOF character is
                > entered.[/color]

                This is easy enough as long as you don't mind the input being buffered.
                [color=blue]
                > 2. I also wanted to add to this by writing another program that prints
                > out a string, ignoring everything that follows a specified character
                > until a new line is found..[/color]

                Okay, there are a few different strategies you can employ to get this
                done, depending on which type of input function, character oriented or
                line oriented you use.
                [color=blue]
                > 3. I know these programs might be getting a bit repetitive for all you
                > experienced C programmers, but I'd like to make a bunch of variations,
                > the next one being this: I'm aware that there is an "ispunct" function,
                > so I wanted to write a program that removes punctuation from input
                > strings.[/color]

                Do you want to actually *remove* punctuation characters from the input
                buffer or simply ignore them when printing out the result?
                [color=blue]
                > 4. Finally, how would I program simple substitutions for specified
                > characters in a string? e.g. say I wanted to substitute every
                > occurrence of "a" with "BANG!"
                >
                > e.g. "I am a newbie to C programming" would turn into
                > "I BANG!m BANG! newbie to C progrBANG!mming "[/color]

                Well the general strategy is to count the occurences of the characters
                you want replaced and calculate the size/length of the resulting
                string, based on the length of the replacing sequences and finally
                allocate sufficient memory, via malloc(). The you copy the original
                string to the allocated buffer, via a loop, and as you encounter the
                characters you need to replace, emit the pertinent sequence instead of
                the character, and loop until you hit the end of the source string.

                Note though that if your resulting string will be of *shorter* size
                than the original string, then you may do the substitutions in situ,
                without allocating memory for another string. The standard C library
                string functions in string.h will be handy.

                Comment

                • janice86@gmail.com

                  #9
                  Re: Really simple string functions

                  Thanks for the replies, everyone. I've managed to get the first part
                  out. I think I didn't make it clear, though, that I actually wanted to
                  modify the one program to include the functionalities I've described
                  (removing punctuation etc) rather than writing seperate programs.

                  For the first part I simply used two getchar statements, one inside a
                  while loop with a != EOF condition.
                  [color=blue]
                  >Okay, there are a few different strategies you can employ to get this[/color]
                  done, depending on which type of input function, character oriented or
                  line oriented you use.

                  I'm using a character-oriented input function, so I guess for the
                  second part I need to search through the input for a given character
                  (e.g. %), then keep using getchar() to search for a '\n' character.
                  Would anyone be able to show me how to implement this functionality
                  into my existing code?
                  [color=blue]
                  >Do you want to actually *remove* punctuation characters from the input[/color]
                  buffer or simply ignore them when printing out the result?

                  I don't exactly understand what you're asking, but all that matters is
                  that the resulting output doesn't contain said punctuation.

                  Comment

                  • juvenuts@hotmail.com

                    #10
                    Re: Really simple string functions

                    Oops, I wrote the above reply using a different account.

                    [color=blue][color=green]
                    >> Please copy and paste the real, compiling programme.[/color][/color]
                    My apologies, I actually didn't have a real, compiling programme at
                    that stage; the code I had was simply a 'skeleton' for what I thought
                    the solution would be.

                    Comment

                    • santosh

                      #11
                      Re: Really simple string functions

                      janice86@gmail. com wrote:[color=blue]
                      > Thanks for the replies, everyone. I've managed to get the first part
                      > out. I think I didn't make it clear, though, that I actually wanted to
                      > modify the one program to include the functionalities I've described
                      > (removing punctuation etc) rather than writing seperate programs.
                      >
                      > For the first part I simply used two getchar statements, one inside a
                      > while loop with a != EOF condition.[/color]

                      If you post your code, the experts in this group, (not me), will be
                      able to bring errors, unportable constructs and other assumptions to
                      your notice.
                      [color=blue][color=green]
                      >> Okay, there are a few different strategies you can employ to get this
                      >> done, depending on which type of input function, character oriented or
                      >> line oriented you use.[/color]
                      >
                      > I'm using a character-oriented input function, so I guess for the
                      > second part I need to search through the input for a given character
                      > (e.g. %), then keep using getchar() to search for a '\n' character.
                      > Would anyone be able to show me how to implement this functionality
                      > into my existing code?[/color]

                      One naive way is:

                      while((c = getchar()) != EOF) {
                      if(c == '%') {
                      printf("\n");
                      break;
                      }
                      else
                      putchar(c);
                      }

                      If you post your attempt others more knowledgeble than me will be able
                      to offer advice.
                      [color=blue][color=green]
                      > >Do you want to actually *remove* punctuation characters from the input[/color]
                      > buffer or simply ignore them when printing out the result?
                      >
                      > I don't exactly understand what you're asking, but all that matters is
                      > that the resulting output doesn't contain said punctuation.[/color]

                      In which case use a FOR loop and test each character of the input
                      string with the ispunct() function. If it returns false, output that
                      character, increment loop counter and continue, otherwise increment
                      counter and loop. Encountering a null character or whatever other
                      mechanism you use to delimit the string, should break out of the loop
                      after printing a final newline character to flush output.

                      Comment

                      • Keith Thompson

                        #12
                        Re: Really simple string functions

                        janice86@gmail. com writes:[color=blue]
                        > Thanks for the replies, everyone. I've managed to get the first part
                        > out. I think I didn't make it clear, though, that I actually wanted to
                        > modify the one program to include the functionalities I've described
                        > (removing punctuation etc) rather than writing seperate programs.[/color]
                        [snip]

                        Please read <http://cfaj.freeshell. org/google/> *before* you post
                        another followup here.

                        --
                        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                        San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                        We must do something. This is something. Therefore, we must do this.

                        Comment

                        Working...