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?
memmove, strcpy - to move regeons
strchr - to find a character (like space) location in the string
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:
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.