Concatenating strings using a variable arguments function - c

I made a program which receives from 2 till 5 strings and concatenate all together using a variable arguments function.
So far the program works OK, but it always show at the end 3 random characters before showing the complete string.
For example:
Please insert number of strings: 3
string 1: car
string 2: bike
string 3: plane
Full string:
=$>carbikeplane
I have made several tweaks to the program trying to find the reason and fix it, however I always get the same result.
The full program is showed below.
Few comments about the program:
I am printing the strings in different parts of the programs because I was trying to locate where the problem was coming. So some of the printf() functions may not have sense.
The main function seems to be fine, the problem is in the function defined later.
NOTE: I'm still learning C, so there may be some code that can/might be creating undefined behavior, if there is, I would appreciate if you can point those out.
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
char *function(int num, ...);
int main(void)
{
char line1[80] = " ", line2[80] = " ", line3[80] = " ", line4[80] = " ", line5[80] = " ";
int count = 0, count2;
char *newStr;
int i;
int status;
do {
fflush(stdin);
printf("\nPlease select the number of strings (max. 5): ");
scanf("%d", &count);
}while(count < 2 && count > 5);
count2 = count;
fflush(stdin);
status = 1;
for( i = 1 ; count > 0; count--, i++)
{
switch(status)
{
case 1:
{
printf("\nInput string[%d]: ", i);
gets(line1);
status = 2;
break;
}
case 2:
{
printf("\nInput string[%d]: ", i);
gets(line2);
status = 3;
break;
}
case 3:
{
printf("\nInput string[%d]: ", i);
gets(line3);
status = 4;
break;
}
case 4:
{
printf("\nInput string[%d]: ", i);
gets(line4);
status = 5;
break;
}
case 5:
{
printf("\nInput string[%d]: ", i);
gets(line5);
status = 6;
break;
}
}
}
printf("\n%s\n%s\n%s\n%s\n%s\n", line1, line2, line3, line4, line5);
/*call the function of variable arguments*/
/*memory allocation of newstr*/
newStr = (char *)malloc(420*sizeof(char) +1);
switch(count2)
{
case 2:
{
newStr = function(2, line1, line2);
break;
}
case 3:
{
newStr = function(3, line1, line2, line3);
break;
}
case 4:
{
newStr = function(4, line1, line2, line3, line4);
break;
}
case 5:
{
newStr = function(5, line1, line2, line3, line4, line5);
}
}
printf("\nThe final string is: \n");
printf("%s", newStr);
return 0;
}
char *function(int num, ...)
{
va_list arg_ptr;
int b;
char *string;
char *curstr;
va_start(arg_ptr, num); /*initialize the arg_ptr*/
string = (char *)malloc(420*sizeof(char) + 1);
*string = " ";
for(b=0; b < num; b++)
{
curstr = va_arg(arg_ptr, char * );
string = strcat(string,curstr);
}
printf("\n%s", string);
va_end(arg_ptr);
return string;
}

The real problem is that you can compile the line: *string = " "; This is not valid anymore and should not compile. Assumingly you put that line there to initialize your string to have an initial value. But this can easily be solved by allocating the string like this:
string = calloc(420, sizeof(char));
ie: use calloc which sets the memory to zero. This way you have a valid string which can be used by strcat.
I'm not telling you to do not use gets or fflush because it is obvious that this is a home assignment and the suggested fgets has its own problems when dealing with the input string. Certainly if you will use gets in production code someone will kick you at that time.
And about casting the return value of malloc again, it's a two sided sword. If you know for sure that you will compile your project as a C project (ie: filename ends in .c and you use gcc to compile) then yes. Do not cast. However in other circumstances, such as naming the files .cpp or compiling with g++ .... well. You will get the error: error: invalid conversion from ‘void*’ to ‘char*’ without the cast. And I have the feeling that at a beginner level, while doing home assignments for school you more or less concentrate on making your code to compile and run, rather than stick to be pedantic. However for the future, it is recommended that you will be pedantic.

Here is a quick and dirty way to concatenate as many strings as the user wants to enter. Simply press ctrl+d when done to end input:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *concat (const char *str1, const char *str2) {
size_t len1 = strlen (str1); /* get lenghts */
size_t len2 = strlen (str2);
char * s = malloc (len1 + len2 + 2); /* allocate s and concat with ' ' */
memcpy (s, str1, len1);
s [len1] = ' ';
memcpy(s + len1 + 1, str2, len2); /* does not include terminating null */
s [len1 + len2 + 1] = '\0'; /* force null termination */
return s;
}
int main (void) {
char *line = NULL; /* pointer to use with getline () */
ssize_t read = 0;
size_t n = 0;
int cnt = 0;
char *str;
printf ("\nEnter a line of text to concatenate (or ctrl+d to quit)\n\n");
while (printf (" input: ") && (read = getline (&line, &n, stdin)) != -1) {
if (line[read-1] == '\n') { /* strip newline */
line[read-1] = 0;
read--;
}
if (cnt == 0) /* if more than 1 word, concat */
str = strdup (line);
else
str = concat (str, line);
cnt++;
}
printf ("\n\n Concatenated string: %s\n\n", str);
return 0;
}
output:
$ ./bin/concat
Enter a line of text to concatenate (or ctrl+d to quit)
input: my dog
input: has lots of fleas
input: my cat
input: has some too.
input:
Concatenated string: my dog has lots of fleas my cat has some too.

This is the modified code working fine
#include <stdio.h>
#include <string.h>
#include<stdlib.h>
#include <stdarg.h>
char *function(int num, ...);
int main(void)
{
char line1[80] = " ", line2[80] = " ", line3[80] = " ", line4[80] = " ", line5[80] = " ";
int count = 0, count2;
char *newStr;
int i;
int status;
do {
fflush(stdin);
printf("\nPlease select the number of strings (max. 5): ");
scanf("%d", &count);
}while(count < 2 && count > 5);
count2 = count;
fflush(stdin);
status = 1;
for( i = 1 ; count > 0; count--, i++)
{
switch(status)
{
case 1:
{
printf("\nInput string[%d]: ", i);
gets(line1);
status = 2;
break;
}
case 2:
{
printf("\nInput string[%d]: ", i);
gets(line2);
status = 3;
break;
}
case 3:
{
printf("\nInput string[%d]: ", i);
gets(line3);
status = 4;
break;
}
case 4:
{
printf("\nInput string[%d]: ", i);
gets(line4);
status = 5;
break;
}
case 5:
{
printf("\nInput string[%d]: ", i);
gets(line5);
status = 6;
break;
}
}
}
printf("\n%s\n%s\n%s\n%s\n%s\n", line1, line2, line3, line4, line5);
/*call the function of variable arguments*/
/*memory allocation of newstr*/
newStr = (char *)malloc(420*sizeof(char) +1);
switch(count2)
{
case 2:
{
newStr = function(2, line1, line2);
break;
}
case 3:
{
newStr = function(3, line1, line2, line3);
break;
}
case 4:
{
newStr = function(4, line1, line2, line3, line4);
break;
}
case 5:
{
newStr = function(5, line1, line2, line3, line4, line5);
}
}
printf("\nThe final string is: \n");
printf("%s", newStr);
return 0;
}
char *function(int num, ...)
{
va_list arg_ptr;
int b;
char *string;
char *curstr;
va_start(arg_ptr, num); /*initialize the arg_ptr*/
string = (char *)malloc(420*sizeof(char) + 1);
//*string = " ";
for(b=0; b < num; b++)
{
curstr = va_arg(arg_ptr, char * );
string = strcat(string,curstr);
}
printf("\n%s", string);
va_end(arg_ptr);
return string;
}
Just included stdlib and commented *string
Have a nice day

Length of your code is proportional to max strings user can enter. That doesn't sound good, right? (what if someone in the future will ask you to change it to allow user to enter 10 strings? 20? 100???).
In such cases usually in help come arrays - instead of having 5 different variables just use an array of them:
So change:
char line1[80], line2[80], line3[80], line4[80], line5[80];
to:
char lines[5][80];
So when in need of setting for e.g. second string, you can get it through:
char* line2 = lines[1]; \\ remember about indexes starting from 0,
\\ so second element has index 1
So now instead of 5 switch cases you can go with:
for( i = 1 ; count > 0; count--, i++)
{
printf("\nInput string[%d]: ", i);
fgets(lines[i], 80, stdin);
}
Moreover you don't need variable arguments function, as you can just pass array and it's size:
char *function(int array_elements, char ** array);
//Usage:
concatenated_string = function(5, lines);
Also it's good practice to put all const values into variables (so when changing max amount of string user can enter, you need to change only one place).
const int MAX_STRINGS = 5;
const int MAX_STRING_LENGTH = 80;
Now comes the real problem:
string = (char *)malloc(420*sizeof(char) + 1);
*string = " ";
Why would you allocate 420 bytes? What if user enters only one string - what do you need the rest 340 bytes for?
To get the length of concatenate strings, iterate through lines (from 0 to array_size), get lengths of lines (with strlen), sum them together and add 1 for trailing '\0'. Now you won't have any unnecessary memory allocated.
Next - *string = " "; should not compile as *string is a char and " " i a string (char *). Do *string = '\0' instead or call calloc instead of malloc.

Related

How do I reallocate a array of structures in a function

I am trying to allocate a dynamic array of Country objects for my school project. I have malloc'd the array in main() function and I am reallocating it in a add_country() function. but it seems to give me realloc invalid ponter error. Could someone help? This is the minimal reproducable code.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int count = 0;
typedef struct test_Country
{
char name[20];
int gold;
int silver;
int bronze;
} test_Country;
test_Country *addtest_Country(test_Country test_Country_obj, test_Country*array)
{
int flag = 0;
printf("%s\n", "before realloc");
test_Country *new_array;
new_array = realloc(array, sizeof(test_Country *) * (count + 1));
printf("%s\n", "after realloc");
//array[count].name = (char *)malloc(strlen(test_Country_obj.name) + 1);
if (count == 0)
{
strcpy(new_array[0].name, test_Country_obj.name);
}
else
{
for (int i = 0; i < count; i++)
{
if (strcasecmp(new_array[i].name, test_Country_obj.name) == 0)
{
printf("%s", "test_Country already added\n");
flag = 1;
break;
}
}
}
if (flag == 0)
{
strcpy(new_array[count].name, test_Country_obj.name);
test_Country_obj.gold = 0;
test_Country_obj.silver = 0;
test_Country_obj.bronze = 0;
new_array[count] = test_Country_obj;
count = count + 1;
}
flag = 0;
return new_array;
}
int main()
{
char choice;
test_Country *array = malloc(sizeof(test_Country));
test_Country test_Country_obj;
printf("%s", "Enter your choice : ");
scanf("%s", &choice);
//fgets(ptr, 80, stdin);
//sscanf(ptr, "%c %s %d %d %d", &choice, test_Country_obj.name, &test_Country_obj.gold, &test_Country_obj.silver, &test_Country_obj.bronze);
//printf("%s", &choice);
while (choice != 'E')
{
printf("%s", "Enter test_Country name : ");
scanf("%s", test_Country_obj.name);
array = addtest_Country(test_Country_obj, array);
//printf("%d%s", count, "is count");
printf("%s", "Enter your choice : ");
scanf("%s", &choice);
}
}
I cant seem to understand what is wrong.
char choice;
scanf("%s", &choice);
is bad. choice has only room for one character, so it can hold only strings upto zero characters. (the one-character room is for terminating null-character). Trying to store strings longer than zero character leads to dangerous out-of-range write and it may destroy data around that.
To avoid out-of-range write, you should allocate enough elements and specify the maximum length to read. The maximum length should be the buffer size minus one for terminating null-character.
char choice[16]; /* allocate enough elements */
scanf("%15s", choice); /* specify the maximum length */
After that, choice in the while and switch should be replaced with choice[0] to judge by the first character. Another way is using strcmp() to check the whole string.

Why does my number variable change after running the 'insert' function?

I have been looking at this code of mine for my schoolwork but I cannot understand why my integer variable has changed after I passed it into a function by value although I did not alter it in the function.
#include <stdio.h>
#include <stdlib.h>
void Q1();
void Q1_Remove (char **ptr_string);
void Q1_Insert (char **ptr_string, int length);
int main()
{
Q1();
return 0;
}
void Q1 () {
int number;
printf("How many characters do you want to input: ");
scanf("%d", &number);
char *string = malloc(number + 1);
printf("Input the string: ");
scanf("%s", string);
printf("The string is: %s\n", string);
int option;
do {
printf("Do you want to 1-Insert, 2-Remove or 3-Quit: ");
scanf("%d", &option);
switch (option) {
case 1:
Q1_Insert(&string, number);
break;
case 2:
Q1_Remove(&string);
break;
}
if (option == 1 || option == 2) {
printf("Resulting string: %s\n", string);
}
} while (option == 1 || option == 2);
free(string);
}
void Q1_Remove (char **ptr_string) {
(*ptr_string)++;
}
void Q1_Insert (char **ptr_string, int length) {
int curr_len = strlen(*ptr_string);
int i;
printf("length is %d and curr_len is %d", length, curr_len);
if (curr_len == length) {
for (i = curr_len - 1; i > 0; --i) {
*(ptr_string + i) = *(ptr_string + i - 1);
}
} else {
(*ptr_string)--;
}
char to_insert;
printf("What is the character you want to insert: ");
scanf(" %c", &to_insert);
**ptr_string = to_insert;
}
My terminal input and output:
How many characters do you want to input: 5
Input the string: abcde
The string is: abcde
Do you want to 1-Insert, 2-Remove or 3-Quit: 1
length is 5 and curr_len is 5
What is the character you want to insert: a
Resulting string: abcde
Do you want to 1-Insert, 2-Remove or 3-Quit: 1
length is 11212224 and curr_len is 5 // length is weird here
What is the character you want to insert:
(EDIT 2)
Removed constant for variable number and added in the declaration on the top. Edited to add in the headers!
Primarily the issue is how you are shuffling characters in the string. You are doing this:
for (i = curr_len - 1; i > 0; --i) {
*(ptr_string + i) = *(ptr_string + i - 1);
}
However, the actual string pointer is *ptr_string. Since you are using ptr_string + i, it's treating ptr_string as an array of pointers, and you are writing all over the stack memory following your string pointer defined in main. This will be what is trashing your number value, among other things.
Instead, you need to do this:
*(*ptr_string + i) = *(*ptr_string + i - 1);
Personally, I'd use array notation instead, which is far more readable:
(*ptr_string)[i] = (*ptr_string)[i - 1];
Or simply scrap the loop entirely and use memmove.

How to split a string into int[3]

I have a string, like "101 1 13" and I need to split it to a int aux[3] --> resulting in aux[0] = 101, aux[1] = 1 and aux[2] = 13 (in this case). How can
I do that?
In the example of the code below I get op as a String and want to get the value of the INTs in there. Each int is divided in the string by a white space(" ").
Another detail: I need the code to compile with flag -std=c99, so the answer that was accepted would not work.
#include <stdio.h>
#include <stdlib.h>
//example of str = "101 1 14" (char *)
// example of output = {101, 1, 14}(int *)
int* stoi(char *str) {
// function to split str into 3 ints
}
int main() {
char op[10];
int num[3];
scanf("%s\n", op);
num = stoi(op);
printf("%d %d %d", num[0], num[1], num[2]);
return 0;
}
First you need to tokenize your input (break apart the input into distinct elements). Then you need to parse/integerize the individual tokens by converting them from strings to the desired format.
Sample Code
#include <stdio.h>
#include <string.h>
#define BUF_LEN (64)
int main(void)
{
char buf[BUF_LEN] = { 0 };
char* rest = buf;
char* token;
int i = 0;
int iArr[100] = { 0 };
if ( fgets(buf, BUF_LEN, stdin) != NULL )
{
strtok(buf, "\n"); // Remove newline from input buffer in case we want to call fgets() again.
while ( (token = strtok_r(rest, " ", &rest)) != NULL )
{
iArr[i] = strtol(token, NULL, 10);
printf("Token %d:[%d].\n", i, iArr[i]);
i++;
}
}
return 0;
}
Sample Run
1231 12312 312 1232 1312
Token 0:[1231].
Token 1:[12312].
Token 2:[312].
Token 3:[1232].
Token 4:[1312].
Try to replace your code by following code.
The new code works only if input contains only single space between integers.
Your code:
while(op[cont] != '\0') {
for(i = 0; op[cont] != ' '; i++, cont++) {
num[i] += op[cont];
}
printf("num[i] = %d\n", num[i]);
}
New code:
while(op[cont] != '\0')
{
if(op[cont] != ' ')
num[i] = num[i]*10 + (op[cont]- '0');
else
i++;
cont++;
}
See this example of how to do that:
char string [10] = "101 1 666"
int v [3], n=0, j=0;
int tam = strlen(string);
int current_Len = 0;
for(i=0; i<tam; i++){
//32 = ascii for White space
if(string[i] != 32){
n = n*10 + string[i] - '0';
current_len++;
} else if (current_len > 0){
v[j++] = n;
current_len = 0;
n=0;
}
}
if (current_len > 0){
v[j++] = n;
}
This answer is assuming you know how much integers your string contain at the time of writing your code. It also uses specific clang/gcc extension (typeof) and may not be portable. But it may be helpful to someone (I mainly wrote it because I had nothing good to do).
#include <stdio.h>
#include <string.h>
struct {int _[3];} strToInt3(const char (*pStr)[])
{
int result[3] = {0}, *pr = result;
for(register const char *p = *pStr; *p; ++p)
{
if(*p == ' ') ++pr;
else
*pr *= 10,
*pr += *p - '0';
}
return *(__typeof__(strToInt3(0)) *)result;
}
int main()
{
char op[10];
int num[3];
scanf("%10[^\n]", op),
//memcpy(num, strToInt3(op)._, sizeof(num));
//or
*(__typeof__(strToInt3(0)) *)num = strToInt3(op);
printf("%d %d %d", num[0], num[1], num[2]);
}
I've commented the copying of returned array using memcpy and added a structure assignment. Although both must be valid (not standard I guess but working in most cases) I prefer the second option (and maybe some compiler optimizers will).
Also I assume ASCII character set for chars.
I found an easier approach to the problem. I insert a scanf, that don't catch the space blanket and convert it using atoi. As it is just 3 ints it doesn't become so bad to use this simple, repetitive way of catching the values. And it work with the -std=c99 flag, that I needed to use.
scanf("%s[^ ]\n", op);
num[0] = atoi(op);
scanf("%s[^ ]\n", op);
num[1] = atoi(op);
scanf("%s[^ ]\n", op);
num[2] = atoi(op);
printf("%d\n", num[0]);
printf("%d\n", num[1]);
printf("%d\n", num[2]);

Replacing a substring in a string - c

I'm trying to do a program which finds a substring in a string and replaces it with another substring entered by user. My code doesn't give a compile or run-time error, but it just doesn't work. I put printfs in the while loop which I wrote a comment line near it, and the program doesn't go into first if -I put another comment line near it. It prints a, h and i. The other parts in loop aren't working. Here's my code:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
char *findAndReplace(char *sentence, char *word1, char *word2);
void main()
{
char sentence[1000];
char word1[200];
char word2[200];
int length;
printf("Please enter a sentence: ");
gets(sentence);
printf("Please write the word to be replaced: ");
gets(word1);
printf("Please write the word to be put instead: ");
gets(word2);
findAndReplace(sentence, word1, word2);
system("pause");
}
char* findAndReplace(char *sentence, char *word1, char *word2)
{
char *search, *tempString[1000];
int a, b, c, d, i = 0, j, sentenceLength, word1Length, searchLength;
sentenceLength = strlen(sentence);
printf("Length of %s is %d\n", sentence, sentenceLength);
printf("Finding ");
puts(word1);
search = strstr(sentence, word1);
searchLength = strlen(search);
word1Length = strlen(word1);
strcpy(tempString, sentence);
if(search != NULL)
{
printf("Starting point: %d\n", sentenceLength - searchLength);
}
else
{
printf("Eşleşme bulunamadı.\n");
}
j = 0;
while(j < sentenceLength + 1) //This loop
{
printf("a");
if(word1[i] == tempString[j])
{
printf("b");
if(i == word1Length)
{
c = j;
printf("c");
for(d = 0; d < word1Length; d++)
{
tempString[c - word1Length + d + 1] = word2[d];
printf("d");
}
i = 0;
j++;
printf("e");
}
else
{ printf("f");
i++;
j++;
}
printf("g");
}
else{
printf("h");
i = 0;
j++;
}
printf("i");
}
puts(tempString);
}
You've made a decent start, but you're making this a lot harder than it needs to be. One way to minimize errors is to rely on standard library functions when there are any that do the work you need done. For example:
char tempString[1000];
char *search;
search = strstr(sentence, word1);
if (search) {
ptrdiff_t head_length = search - sentence;
int sentence_length = strlen(sentence);
int word1_length = strlen(word1);
int word2_length = strlen(word2);
if (sentence_length + word2_length - word1_length < 1000) {
/* construct the modified string */
strncpy(tempString, sentence, head_length);
strcpy(tempString + head_length, word2);
strcpy(tempString + head_length + word2_length, search + word1_length);
/* copy it over the original (hope it doesn't overflow!) */
strcpy(sentence, tempString);
} else {
/* oops! insufficient temp space */
}
} /* else the target word was not found */
That covers only the search / replacement bit, fixing the error in tempString's type first pointed out by iharob. Also, it replaces only the first occurrence of the target word, as the original code appeared to be trying to do.
Among other things you have declared tempString as char* tempString[1000] which is an array of uninitialized character pointers so when you do
strcpy(tempString, sentence);
you are basically getting undefined behavior.
Use also fgets instead of gets when you input strings - even though you have rather large buffers it can happen one day that you pipe in a text file and get a stack overflow.
If I were you I would use strtok and split your sentence in words, then check each word. If word is same replace otherwise add sentence word to a new string.
e.g.
char newString[1000] = {0};
for (char* word = strtok(sentence, " "); word != NULL; word = strok(NULL, " "))
{
if (!strcmp(word, word1)) // here you may wanna use strncmp or some other variant
{
strcat(newString, word2);
}
else
{
strcat(newString, word);
}
strcat(newString, " ");
}
newString[strlen(newString)-1] = '\0';

Remove spaces from an array in C?

I am trying to remove the spaces from my array "secuencia", the users give me this entry:
"1 2 3 4 5 6 7 8 9"
I want to remove the spaces, and save it in another array for later. Then, convert to integer with "ATOI" like I do with the arrays "palancas" and "palancaroja". Those two arrays only contained one number, so I had no problem with them.
please help me... I am programming in ANSI C.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, const char * argv[])
{
char palancas [20000];
int palancai;
char palancaroja[10];
int palancarojai;
char secuencia[20000];
char temp[20000];
int j = 0;
printf("Dame El Numero De Palancas:");
fgets(palancas, 20000, stdin);
printf("Dame La Posision De La Palanca Roja:");
fgets(palancaroja, 10, stdin);
palancai = atoi(palancas);
palancarojai = atoi(palancaroja);
printf("Dame La cadena");
fgets(secuencia, 20000, stdin);
for (int i = 0; i < palancai; i++) {
if (secuencia [i] != ' ') {
temp [i] = secuencia [i];
printf("%s", temp);
}
}
}
This is the simplest way to remove spaces from a string.
char *SourcePtr = secuencia;
char *TargetPtr = SourcePtr;
while (*SourcePtr != 0)
{
if (*SourcePtr != ' ')
{
*TargetPtr = *SourcePtr;
TargetPtr += 1;
}
SourcePtr += 1;
}
*TargetPtr = 0;
Translated version of critical section
for (int i = 0; i < length; i++) {
if (source[i] != ' ') {
temp[i] = source[i];
printf("%s", temp);
}
}
This code copies every character from the array source to the array temp, but simply skips spaces. So if temp is initialized with XXXXX and source is A B C, then temp is AXBXC after the execution of the loop.
You have use two indexes (see other answer)
#include <stdio.h>
//copy to d from s removed space
void remove_space(char *d, const char *s){
for(;*s;++s){
if(*s != ' ')
*d++ = *s;
}
*d = *s;
}
int main(){//DEMO
char secuencia[] = "1 2 3 4 5 6 7 8 9";
char temp[sizeof(secuencia)];
remove_space(temp, secuencia);
puts(temp);//123456789
return 0;
}
You could use strtok and tokenize the string that you get with the delimiter string being " ".
In other words:
char * tok;
int i = 0;
tok = strtok(secuencia, " ");
while(tok != NULL){
temp[i] = tok[0];
i++;
tok = strtok(NULL, " ");
}
This would only work if it's guaranteed that it's a single digit between each space though. Another way to copy it would be to use another loop, cycling through strtok until '\0' is reached, or using strcpy.
first I think that your for loop is looking at the wrong variable. you are trying to loop on palancai where really you want to loop on secuencia.
Below you can find a function that will parse your int.
int MyIntParse(char* str)
{
int iReturn = 0;
for(int i=0;i<20000;++i)
{
iReturn *=10;
switch(str[i])
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '0':
iReturn = iReturn + (str[i] - '0');
break;
}
}
return iReturn;
}

Resources