Thread: Reading Input from file

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    3

    String

    I have a line of text stored as a string, and I scan through it with a loop, when I reach a space I want to move the word before the space to the back of the line and shift everything forward. Is there a function in c which shifts strings to the left, or cuts off an amount of letters from the front?
    Last edited by Horox; 01-30-2008 at 10:12 PM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    memmove, strcpy - to move regeons
    strchr - to find a character (like space) location in the string
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I wrote this program. Pretty straight forward.

    There isn't a built-in function, per se. However, you can achieve what you want to do with selective placement of the null term character and by using two pointers to the array.

    Let's say you have a string you just read in via fgets().

    I like programming\r\n

    You want to first remove the CRLF from the end. (or just LF if you are Unix).
    Now you'll have this: (because you overwrote the line ending(s) with a zero.)

    I like programming\0

    print that line.

    Now, find the first blank. strchr() is very good for that. It will point to the blank between the "I" and the "Like". Variable string is your pointer to the array. And, for conversation purposes, let's say blank_pointer is your second pointer. It points to the blank you just found with strchr().

    Put a \0 where the blank is, like this:

    I\0like programming\0

    Now, you can print out the "like programming" via [i]blank_pointer+1[/b], and follow that by printing the "I" via your string pointer.

    Before you move on to find the next blank, put the blank back between the "I" and "Like". Then, bump up blank_pointer one byte and search for the next blank from blank_pointer on. Repeat until no more blanks are found.

    Todd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM