stripping punctuation from input file stream

This is a discussion on stripping punctuation from input file stream within the C++ Programming forums, part of the General Programming Boards category; hi, I'm supposed to input a string from a file, strip its punctuation from it , and put its letters ...

  1. #1
    asdfasdfasdf
    Guest

    stripping punctuation from input file stream

    hi,
    I'm supposed to input a string from a file, strip its punctuation from it , and put its letters into an array. Is there some type of string function that can do that?

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,668
    Is there some type of string function that can do that?
    No. Have you tried anything yet? I'll give you a hint:


    -create an array of the same length +1 of the original.
    -loop through the string, and if you don't encounter punctuation(use ispunct() or write it by hand - easy), then copy to the second string.
    -null terminate the second string.
    -copy string 2 into string 1.



    ITSA
    Socket Library!

  3. #3
    Peace
    Join Date
    Aug 2001
    Posts
    1,510
    Not to my knowledge. Read the string on character at a time into the new string using a loop. i.e.

    Code:
    char string[50];
    char output[50];
    
    //read to string here
    
    int a = 0;
    for(int i = 0; i < strlen(string); i ++)
    {
       //you could do this several ways. One is to check for a valid character value:
       if ((string[i] >= 97 && string[i] <= 122) || //lowercase a-z
          (string[i] >= 65 && string[i] <= 90)) //upercase a-z
          {
             output[a] = string[i];
             a ++;
          }
    }
    
    OR
    
       //Alternatly you could check in the for loop like this by weeding out specific punctuation instead of only allowing characters
       if (string[i] != '.' && string[i] != '!' && string[i] != '?') //etc, etc...
       {
          output[a] = string[i];
          a ++;
       }
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  4. #4
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    check out ispunct( int ), found in <ctype.h>. It could be useful...
    Couldn't think of anything interesting, cool or funny - sorry.

  5. #5
    asdfasdfasdf
    Guest

    Wink thank you for the help

    I fixed it. Cant believe I didn't think of this stuff before!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  3. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 08:31 PM
  4. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  5. Getting an input stream to stop creating a file
    By Stevek in forum C++ Programming
    Replies: 3
    Last Post: 03-21-2003, 03:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21