C - converting string (sentence) into strings list - c

I need to copy a sentence, for example "Hello world" into a string list, meaning into a char array where every 2 words are seperated by a '\0'.
note that word is defined as any amount of characters in a row without a white spaces.
So whenever my program detects any amount of white spaces in a row (including only 1 white space) it should wtire a single '\0' instead.
the problem is that after writting '\0' for the first time in my target char array, I can't write to it anymore. I guess its because '\0' means end of string but in my case I'm trying to implement a string list inside char array so I must have '\0' between every 2 words.
Basically my question is how can I countinue writing to char array after putting '\0'?
Here is my code so far (as you can see I also check for enough space in traget at every iteration but that part works fine so isn't really intersting)
int strListFromWords(const char* source, char* target, int buffSize)
{
if (buffSize < 2) return -1;
char* sCurrentPointer = source;
char* tCurrentPointer = target;
int charsInTarget = 0;
while (*sCurrentPointer != '\0') // While not end of string
{
if (charsInTarget + 2 < buffSize) // if there is enough space in target for current char
{
charsInTarget++;
if (!isspace(*sCurrentPointer)) // if current char isn't space
{
*tCurrentPointer = *sCurrentPointer;
sCurrentPointer++;
tCurrentPointer++;
}
else
{
*tCurrentPointer = '\0'; // PROBLEMATIC LINE put '\0' instead of spcace (in target)
sCurrentPointer++; // goto next char in source
tCurrentPointer++; // goto next position in target
while (isspace(*sCurrentPointer)) // while there are more spaces in a row
{
sCurrentPointer++; // just skip them without messing with target
}
}
}
else
{ // Not enough space
emptyStrList(target);
return 0;
}
}
*tCurrentPointer = '\0';
*(tCurrentPointer + 1) = '\0';
return numStrsInList(target);
}
Thank you,

There is nothing to prevent you to write past 0.
I tested your function with the following snippet, and it returns word_count correctly. The target buffer will contain the 0 terminated words, plus an extra 0 at the end. I guess, that was the intention.
#include <conio.h> // for getch()
#include <malloc.h>
#include <string.h>
int main()
{
char* source = " Hello World!\nThis is line number two.\n\n \n \n This is the last line";
size_t buflen = strlen(source);
char* target = (char*)malloc(strlen(source));
int word_count = strListFromWords(source, target, buflen);
printTarget(target);
free(target);
getch();
}
This function will show you the whole target buffer:
void printTarget(const char* target) {
char prev = ' ';
for (int i = 0;; i++) {
if (target[i])
putch(target[i]);
else {
putch('\n');
if (!prev)
break;
}
prev = target[i];
}
}
Some minor changes were necessary, to make it compile:
#include <stdio.h>
#include <ctype.h>
int strListFromWords(const char* source, char* target, int buffSize)
{
if (buffSize < 2) return -1;
char* sCurrentPointer = (char*)source;
char* tCurrentPointer = target;
int charsInTarget = 0;
int numStrsInList = 0;
while (*sCurrentPointer != '\0') // While not end of string
{
if (charsInTarget + 2 < buffSize) // if there is enough space in target for current char
{
charsInTarget++;
if (!isspace(*sCurrentPointer)) // if current char isn't space
{
*tCurrentPointer = *sCurrentPointer;
sCurrentPointer++;
tCurrentPointer++;
}
else
{
*tCurrentPointer = '\0'; // PROBLEMATIC LINE put '\0' instead of spcace (in target)
numStrsInList++;
sCurrentPointer++; // goto next char in source
tCurrentPointer++; // goto next position in target
while (isspace(*sCurrentPointer)) // while there are more spaces in a row
{
sCurrentPointer++; // just skip them without messing with target
}
}
}
else
{ // Not enough space
//emptyStrList(target);
return 0;
}
}
*tCurrentPointer = 0;
*(tCurrentPointer + 1) = 0;
return numStrsInList;
}
Pls note, I addressed only what was asked.

I suppose the main problem lies in the formulation of the requirement.
If the requirement is "split a sentence into words", then the result should be an array of "words", meaning an array of strings. If this were the requirement, then the function should have a signature like char **getWordsArrayFromSentence(const char* sentence). As you come up with a different signature, I think that your requirement is something different.
The signature of your method is int strListFromWords(const char* source, char* target, int buffSize), which indicates that it is about copying from source to target while replacing every sequence of white spaces with a single delimiter.
If you chose, for example, character ; as delimiter, then the result for sentence "Hello world" should be "Hello;world"; You can print the result, e.g. with printf("%s", target), and can check whether your algorithm works fine.
However, if you chose string termination character '\0' as delimiter, then the result looks as if it only contained the first word (although the remainder of target will contain the other words as well): target would be "Hello\0world\0" with \0 standing for string termination character. When you then print out target with printf("%s", target), then the output is Hello, i.e. the content of target until the first string termination character.
Hence, signature int strListFromWords(const char* source, char* target, int buffSize) yields a single consolidated sequence of characters but not a "list" of words; the "words" are actually contained in target, but you do not have a data structure that lets you directly access each word at its beginning.
BTW: note that the following lines are problematic,
*tCurrentPointer = "\0";
*(tCurrentPointer + 1) = "\0";
because you assign to *tCurrentPointer, which is a character within target, a pointer value, i.e. the pointer to string "\0"; Instead, you should write
*tCurrentPointer = '\0';
*(tCurrentPointer + 1) = '\0';
(Note the single quotes).

You were not very far. Ok there are still some problems to fix:
int strListFromWords(const char* source, char* target, int buffSize)
{
if (buffSize < 2) return -1;
//char* sCurrentPointer = source; lose const qualifier
const char* sCurrentPointer = source; // better!
or even better:
int strListFromWords(const char* sCurrentPointer, char* target, int buffSize)
{
if (buffSize < 2) return -1;
char* tCurrentPointer = target;
and mainly:
/* *tCurrentPointer = "\0";
*(tCurrentPointer + 1) = "\0"; NO! "\0" is a char ARRAY */
*tCurrentPointer = '\0';
*(tCurrentPointer + 1) = '\0';
But apart from that you code does what it is expected to... The terminating '\0' does not lock the array. It just mark an end of string that will be used but all string functions, but provided you are still inside the array you can write past the '\0'.
You can control it with that code:
int numStrsInList(char *target) {
int n = 0;
while (*target) {
target += strlen(target) + 1; // skip past the '\0'
n += 1;
}
return n;
}
int strListFromWords(const char* source, char* target, int buffSize)
...
int main() {
char target[32];
char src[] = "Hello to the world";
int n;
char *ix = target;
n = strListFromWords(src, target, sizeof(target));
printf("%d words:", n);
while (*ix) {
printf(" >%s<", ix);
ix += strlen(ix) + 1;
}
putchar('\n');
return 0;
}
This outputs as expected:
4 words: >Hello< >to< >the< >world<

*tCurrentPointer = "\0";
*tCurrentPointer has type char; you cannot assign an array (or a pointer after the automatic conversion) to a char.
I suggest you turn on all your compiler warnings and heed them.

Related

Returning the rest of a string in c after the first white space

I'm writing a program to return a the rest of a string after the first white space.
"I had a bad day"
should return
"had a bad day"
This is what I have so far
char* next_word(char* str){
char s[100];
int index = 0;
int i = 0;
int counter = 0;
for(i = 0; i < strlen(str); i++){
if(str[i] == ' '){
index = i+1;
for(index; index < strlen(str); index++){
s[counter] = str[index];
counter = counter + 1;
}
s[index] = '\0';
return s;
}
}
return index;
}
I'm looping through the char* str and finding the index of the first empty space then from there I've made another loop to print out the rest of the string starting at index + 1. For some reason when I writes s[counter] = str[index], I don't believe that its copying the char from str to s.
When I try to return s after the loop I don't get anything. Is it possible to add char to the empty char s[100] then return the full string as s?
Thank You
Your next_word() function is returning a local (to the function) variable which results in a undefined behavior. You must take s (in your case) as input or malloc a character buffer in the function. Then you can do the copying. I prefer you go for the first alternative and do not forget to check the length of the input string, so that you do not cross the size while copying.
Also, the next_word() returns index when no space found? That is clearly a mistake and your code will fail to compile.
For the code, you can just break from the first loop whenever you find the first space and from there you can continue with copying.
You should not return s as it is a local variable on the stack. You could simply return a pointer into the str argument since str remains valid at the time of return.
#include <string.h>
const char* TheString = "I had a bad day";
const char* stringAfterBlank(const char* str)
{
const char* blank = strchr(str, ' ');
if (blank != NULL)
{
return ++blank;
}
return "";
}
void main(int argc, char** argv)
{
const char* restOfTheString = stringAfterBlank(TheString);
// restOfTheString is "had a bad day" pointing into TheString
}
If you need a copy of the string then you can use strdup. If you do then don't forget to free.
You shouldn't return your local variable. The easiest way to accomplish what you want is operating on pointers.
There is solution using only stdio.h, as you wanted:
#include <stdio.h>
char* next_word(char* str);
int main()
{
char* arg = "I had a bad day!";
//Print every "next_word"
char* words = arg;
do{
printf("%s\n", words);
} while(words = next_word(words));
}
char* next_word(char* str)
{
while(*str != '\0'){
if(*str++ == ' ')
return str;
}
return NULL;
}

Why does my string_split implementation not work?

My str_split function returns (or at least I think it does) a char** - so a list of strings essentially. It takes a string parameter, a char delimiter to split the string on, and a pointer to an int to place the number of strings detected.
The way I did it, which may be highly inefficient, is to make a buffer of x length (x = length of string), then copy element of string until we reach delimiter, or '\0' character. Then it copies the buffer to the char**, which is what we are returning (and has been malloced earlier, and can be freed from main()), then clears the buffer and repeats.
Although the algorithm may be iffy, the logic is definitely sound as my debug code (the _D) shows it's being copied correctly. The part I'm stuck on is when I make a char** in main, set it equal to my function. It doesn't return null, crash the program, or throw any errors, but it doesn't quite seem to work either. I'm assuming this is what is meant be the term Undefined Behavior.
Anyhow, after a lot of thinking (I'm new to all this) I tried something else, which you will see in the code, currently commented out. When I use malloc to copy the buffer to a new string, and pass that copy to aforementioned char**, it seems to work perfectly. HOWEVER, this creates an obvious memory leak as I can't free it later... so I'm lost.
When I did some research I found this post, which follows the idea of my code almost exactly and works, meaning there isn't an inherent problem with the format (return value, parameters, etc) of my str_split function. YET his only has 1 malloc, for the char**, and works just fine.
Below is my code. I've been trying to figure this out and it's scrambling my brain, so I'd really appreciate help!! Sorry in advance for the 'i', 'b', 'c' it's a bit convoluted I know.
Edit: should mention that with the following code,
ret[c] = buffer;
printf("Content of ret[%i] = \"%s\" \n", c, ret[c]);
it does indeed print correctly. It's only when I call the function from main that it gets weird. I'm guessing it's because it's out of scope ?
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define DEBUG
#ifdef DEBUG
#define _D if (1)
#else
#define _D if (0)
#endif
char **str_split(char[], char, int*);
int count_char(char[], char);
int main(void) {
int num_strings = 0;
char **result = str_split("Helo_World_poopy_pants", '_', &num_strings);
if (result == NULL) {
printf("result is NULL\n");
return 0;
}
if (num_strings > 0) {
for (int i = 0; i < num_strings; i++) {
printf("\"%s\" \n", result[i]);
}
}
free(result);
return 0;
}
char **str_split(char string[], char delim, int *num_strings) {
int num_delim = count_char(string, delim);
*num_strings = num_delim + 1;
if (*num_strings < 2) {
return NULL;
}
//return value
char **ret = malloc((*num_strings) * sizeof(char*));
if (ret == NULL) {
_D printf("ret is null.\n");
return NULL;
}
int slen = strlen(string);
char buffer[slen];
/* b is the buffer index, c is the index for **ret */
int b = 0, c = 0;
for (int i = 0; i < slen + 1; i++) {
char cur = string[i];
if (cur == delim || cur == '\0') {
_D printf("Copying content of buffer to ret[%i]\n", c);
//char *tmp = malloc(sizeof(char) * slen + 1);
//strcpy(tmp, buffer);
//ret[c] = tmp;
ret[c] = buffer;
_D printf("Content of ret[%i] = \"%s\" \n", c, ret[c]);
//free(tmp);
c++;
b = 0;
continue;
}
//otherwise
_D printf("{%i} Copying char[%c] to index [%i] of buffer\n", c, cur, b);
buffer[b] = cur;
buffer[b+1] = '\0'; /* extend the null char */
b++;
_D printf("Buffer is now equal to: \"%s\"\n", buffer);
}
return ret;
}
int count_char(char base[], char c) {
int count = 0;
int i = 0;
while (base[i] != '\0') {
if (base[i++] == c) {
count++;
}
}
_D printf("Found %i occurence(s) of '%c'\n", count, c);
return count;
}
You are storing pointers to a buffer that exists on the stack. Using those pointers after returning from the function results in undefined behavior.
To get around this requires one of the following:
Allow the function to modify the input string (i.e. replace delimiters with null-terminator characters) and return pointers into it. The caller must be aware that this can happen. Note that supplying a string literal as you are doing here is illegal in C, so you would instead need to do:
char my_string[] = "Helo_World_poopy_pants";
char **result = str_split(my_string, '_', &num_strings);
In this case, the function should also make it clear that a string literal is not acceptable input, and define its first parameter as const char* string (instead of char string[]).
Allow the function to make a copy of the string and then modify the copy. You have expressed concerns about leaking this memory, but that concern is mostly to do with your program's design rather than a necessity.
It's perfectly valid to duplicate each string individually and then clean them all up later. The main issue is that it's inconvenient, and also slightly pointless.
Let's address the second point. You have several options, but if you insist that the result be easily cleaned-up with a call to free, then try this strategy:
When you allocate the pointer array, also make it large enough to hold a copy of the string:
// Allocate storage for `num_strings` pointers, plus a copy of the original string,
// then copy the string into memory immediately following the pointer storage.
char **ret = malloc((*num_strings) * sizeof(char*) + strlen(string) + 1);
char *buffer = (char*)&ret[*num_strings];
strcpy(buffer, string);
Now, do all your string operations on buffer. For example:
// Extract all delimited substrings. Here, buffer will always point at the
// current substring, and p will search for the delimiter. Once found,
// the substring is terminated, its pointer appended to the substring array,
// and then buffer is pointed at the next substring, if any.
int c = 0;
for(char *p = buffer; *buffer; ++p)
{
if (*p == delim || !*p) {
char *next = p;
if (*p) {
*p = '\0';
++next;
}
ret[c++] = buffer;
buffer = next;
}
}
When you need to clean up, it's just a single call to free, because everything was stored together.
The string pointers you store into the res with ret[c] = buffer; array point to an automatic array that goes out of scope when the function returns. The code subsequently has undefined behavior. You should allocate these strings with strdup().
Note also that it might not be appropriate to return NULL when the string does not contain a separator. Why not return an array with a single string?
Here is a simpler implementation:
#include <stdlib.h>
char **str_split(const char *string, char delim, int *num_strings) {
int i, n, from, to;
char **res;
for (n = 1, i = 0; string[i]; i++)
n += (string[i] == delim);
*num_strings = 0;
res = malloc(sizeof(*res) * n);
if (res == NULL)
return NULL;
for (i = from = to = 0;; from = to + 1) {
for (to = from; string[to] != delim && string[to] != '\0'; to++)
continue;
res[i] = malloc(to - from + 1);
if (res[i] == NULL) {
/* allocation failure: free memory allocated so far */
while (i > 0)
free(res[--i]);
free(res);
return NULL;
}
memcpy(res[i], string + from, to - from);
res[i][to - from] = '\0';
i++;
if (string[to] == '\0')
break;
}
*num_strings = n;
return res;
}

Removing spaces from a string

I am trying to copy from one string to another, but the second string should omit the space. I tried to approach this by saying, if a character in the original string is a space, do not copy it. Instead, copy the next character. However, the program deletes everything after the space. Any ideas?
char deleteSpaces(char phrase[256], int length){
int j, i=0;
char phrase2[length];
for(j=0;j<length;j++){
if(phrase[i]==' '){
phrase2[j]=phrase[i+1];
i++;
}
phrase2[j]=phrase[i];
}
return phrase2;
}
Here is a solution:
void deleteSpaces(char src[], char dst[]){
// src is supposed to be zero ended
// dst is supposed to be large enough to hold src
int s, d=0;
for (s=0; src[s] != 0; s++)
if (src[s] != ' ') {
dst[d] = src[s];
d++;
}
dst[d] = 0;
}
First, you're returning a static tab and this is wrong.
Secondly you don't increment 'i' if there is no space.
And to finish you will copy spaces if there is more than one space in a row. And you do not control if you reach the end of your source.
for(j = 0; j < length; j++)
{
while (src[i] == ' ' && i < length) i++;
if (i < length)
dest[j] = src[i++];
else
{
dest[j] = 0;
break;
}
}
My solution. Arguments and their order are chosen to match strncpy().
#include <ctype.h>
char *strip_whitespace(char *dest, const char *src, size_t n)
{
char *s, *d;
/*
* Copy 'src' to 'dest', omitting whitespace and making sure we don't
* overflow 'dest'.
*/
for(s=src, d=dest; *s && (d-dest)<n; s++) {
if( !isspace(*s) ) {
*d = *s;
d++;
}
}
/* Ensure that dest is NUL terminated in any event */
if( d-dest < n ) {
*d = '\0';
} else {
dest[n-1] = '\0';
}
return dest;
}
As pointed out already you need to allocate a new string and return a pointer to it. This code works:
char* strip (char* input)
{
int loop;
char *output = (char*) malloc (strlen(input));
char *dest = output;
if (output)
{
for (loop=0; loop<strlen(input); loop++)
if (input[loop] != ' ')
*dest++ = input[loop];
*dest = '\0';
}
return output;
}
int main (void)
{
char srcString[] = " this is a test with spaces at the end ";
char* dstString = strip (srcString);
printf ("source string = '%s'\n", srcString);
printf (" dest string = '%s'\n", dstString ? dstString : "malloc failed in strip");
free (dstString);
return 0;
}
Output:
source string = ' this is a test with spaces at the end '
dest string = 'thisisatestwithspacesattheend'
It takes the input string and allocates a destination string the same size which is safe, although wasteful of a few bytes.
The method is simple; only copy a character if it is not a space. After all the characters are copied, I write the terminator on the end and return the pointer.
****WHAT YOU WERE DOING** IS THAT if a character in the original string is a space, do not copy it. Instead, copy the next character. IS NOT A GOOD idea because
Case 1. When multiple spaces are present, it simply discard the first spaces and copies the second one...
Case 2: Your programme copies the string only after space is found,,it is simply skipping the first word of string which doesn't start with spaces.
case 3. You are only returning the character pointed by phrase2[0] as return type is char,and the scope of local variable is limited to only that function....
//the corrected programme
int deleteSpaces(char phrase[256],charphrase2[256])
{
int i,j;
i=j=0;
while(phrase[i]!=NULL){
if(phrase[i]!=' '){
phrase2[j]=phrase[i];
j++;
}
i++;
}//end while
phrase2[j]=phrase[i] //nulcharceter copied
return 0;
}//end deleteSpaces function
I use this:
char input[100], foreval[100];
for(i=0;i<strlen(input);i++){
if(isspace(input[i])){
continue;
}
strncat(foreval,&input[i],1);
}

C - Replacing char with string

I am writing a program that encodes text such that it can be put into a URL. I have the user inputting a string and if it contains special characters (#, %, &, ?, etc.) to replace them with their corresponding character codes (%23, %25, %26, %3F, etc.). The problem is that the special characters are only of length 1 and the codes are of length 3. The codes end up replacing characters after the special one. This is the code I am using to do the replacement.
char *p = enteredCharStr;
while ((p = strstr(p, specialCharArr[x])) != NULL )
{
char *substr;
substr = strstr(enteredCharStr, specialChar[x]);
strncpy(substr, charCodesArr[x], 3);
p++;
}
Example output from using my program with input: "this=this&that"
this%3Dis%26at
I would like the output to be:
this%3Dthis%26that
Any idea on how to implement what I am trying to do in C (no libraries)?
One way to approach this problem would be to allocate a second string that is three times as large as enteredCharStr and copy the characters over one by one and when you see special character write the replaement instead. You want it to be three times as large since in the worst case you need to replace nearly all the characters.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int isspecial(int c){
static char table[] = "#%&?=<>"; //add etc..
return strchr(table, c) ? 1 : 0;
}
char *encode(const char *s){
size_t capa = 1024;
char *buff=malloc(capa*sizeof(char));
size_t size = 0;
for(;*s;++s){
if(size + 3 > capa){
capa += 32;
buff = realloc(buff, capa*sizeof(char));
}
if(isspecial(*s)){
size+=sprintf(buff+size, "%%%02x", *s);
} else {
size+=sprintf(buff+size, "%c", *s);
}
}
if(size == capa){
buff=realloc(buff, (size+1)*sizeof(char));
}
buff[size++]='\0';
return realloc(buff, size*sizeof(char));
}
int main(void){
char *enteredCharStr = "this=this&that";
char *p = encode(enteredCharStr);
printf("%s\n", p);
free(p);
return 0;
}
You need to make a new string. Here's an example:
char *str = "abc$ddd";
char *p = str;
char *buf = malloc(strlen(str)+1);
char *pbuf = buf;
while(*p) {
if(*p != '$') *pbuf ++ = *p;
p++;
}
It will copy from str to buf all non-$,byte per byte.
Note that in your case,you need to perform the right computation of size of new string.
A C 'string' is a fixed-size array of characters, and therefore there is no built-in notion of insertion. You're effectively asking how to insert n characters into the middle of an array.
One strategy come to mind:
To insert a string of length x at position i of an array of length n:
Resize the array to size n+x (using something like realloc).
Shuffle every character beyond position i to position i+x.
Write your string into the x positions now freed by this shuffle operation.
Alternatively, allocate a new array that is big enough to hold your target string (i.e., with all the substitutions applied), and then write your result into that by copying from the target array until you encounter a character you'd like to replace, then copy from the replacement string, then continue reading from the original source array.
I'm copying characters over one by one, and if I see a special character, (In this code only "#")
I copy in 3 characters, incrementing the index into the output buffer by 3.
You can also do something smarter to guess the buffer size, and perhaps loop over the entire operation, doubling the size of the buffer each time it overruns.
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char* argv[]){
if (argc != 2) {
exit(1);
}
char* input = argv[1];
int bufferSize = 128;
char* output = malloc(bufferSize);
int outIndex = 0;
int inIndex = 0;
while(input[inIndex] != '\0'){
switch (input[inIndex])
{
case '#':ยท
if(outIndex + 4 > bufferSize){
// Overflow, retry or something.
exit(2);
}
output[outIndex] = '%';
output[outIndex+1] = '2';
output[outIndex+2] = '3';
outIndex = outIndex + 3;
inIndex = inIndex + 1;
break;
// Other cases
default:
if(outIndex + 2 > bufferSize){
exit(2);
}
output[outIndex] = input[inIndex];
outIndex = outIndex + 1;
inIndex = inIndex + 1;
break;
}
}
output[outIndex] = '\0';
printf("%s\n", output);
return 0;
}

How to concatenate two strings where the source string should be appended before the destination string?

I'm stuck at yet another C problem. How can I concatenate two strings with the second string being inserted before the first string?
This is what I came up with. Unfortunately I'm stuck at all these pointer to chars, char arrays et cetera.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char* argv[] )
{
char* output;
int i;
for(i = 9; i > 0; i--)
{
unsigned int value = (unsigned int)i;
char buffer[20];
sprintf(buffer, "%u", value);
// strcat(ouput, buffer); // append before the string.
// first loop: 9
// second loop: 89
// third loop: 789
}
printf("%s", output);
}
How must I correct my code to make it work? I guess I have to somehow set the output variable to empty. When do I need fixed widths for the char array or the pointer? 20 was just a random guess from me.
I'm very confused, as your posted code has absolutely nothing to do with the problem you state. (Well, they both use strings, but that's about it)
char* src = "Hello, ";
char* dest = "World!";
char* temp;
temp = malloc(strlen(src) +strlen(dest) + 1);
strcpy(temp, src);
strcat(temp, dest);
dest = temp;
Unless dest is a fixed buffer of adequate size for the combined string. If so, then replace the last line with:
strcpy(dest, temp);
free(temp);
Now, if you want to specifically build the list of digits backwards, let's try a different tack:
char buffer[10];
buffer[9] = '\0'; // null terminate our string.
char* output;
int i;
for(i = 9; i > 0; i--)
{
// this is a fast way of saying, sprintf("%u", i);
// works only for single digits
char d = (char)('0' + i);
buffer[i-1] = d;
output = &buffer[i-1];
printf("%s", output);
}
Usually, you should just avoid the situation to start with. The most obvious solution for your example would be to simply count upward to start with. When that's not suitable, a recursive solution to reverse the order in which the string is built can still allow you to generate the string from beginning to end:
int build_string(int value, char *string) {
char temp[10];
if (value > -1)
build_string(value-1, string);
sprintf(temp, "%d", value); // use snprintf if available.
strcat(string, temp);
return string;
}
int main() {
char result[20] = {0};
build_string(9, result);
printf("%s", result);
return 0;
}
You can append the integer at the end of the string as:
int i;
char buffer[20];
for(i = 0; i < 10; i++) {
sprintf(buffer+i, "%u", i);
}
printf("%s", buffer); // prints 0123456789
For your stated problem (insert one string in front of another), this code will do the job - but has no error checking. It assumes there is enough space in the target buffer for the existing string and the new prefix:
/* Insert string t in front of string s in string s */
char *strinsert(char *s, const char *t)
{
char *p = s + strlen(s);
char *q = p + strlen(t);
char *r = s;
while (p >= s)
*q-- = *p--;
while (*t)
*s++ = *t++;
return(r);
}
What it does is copy the existing string up by the correct number of places so that there is space for the new string at the beginning.
Assuming that the destination buffer is big enough and that the source and destination do not overlap:
// not sure what order to put the params - the usual C way is destination
// followed by source, but it's also potentially confusing that the result of
// prepend(foo,bar) is "<bar><foo>".
char* prepend(char *restrict dest, const char *restrict src) {
size_t len = strlen(src);
memmove(dest + len, dest, strlen(dest));
return memcpy(dest, src, len);
}
If the buffers may overlap (for example, if src is the second half of dest), this approach doesn't work.
If the destination buffer is not big enough, then someone has to allocate new memory for the result, in which case the question of which is the "source" and which the "destination" disappears - they're both "source" and neither is "destination".

Resources