I have a char* like this "1 10 14 16"
I want to split it to integer array like 1,10,14,16...
how i do that?
I suggest using strtol in a loop, using explicitly its endptr argument, something like:
char* pc = input_string;
char* end = NULL;
while (*pc) {
long l = strtol (pc, &end, 0);
if (end == pc) break;
do_something_useful_with_the_number (l);
pc = end;
}
if you want to accumulate all the numbers in an array, replace do_something_useful_with_the_number (l); with code growing that array (probably using malloc and/or realloc ...)
I would use strtok() like this:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main() {
char string[] = "1 10 14 16",
*next = NULL;
int number[10] = {0},
noOfNumbers = 0,
i = 0;
next = strtok(string, " "); /* first call must have the string and delimiters */
while ( next != NULL ) {
number[noOfNumbers++] = atoi(next);
next = strtok(NULL, " "); /* second call only the delimiters */
}
for ( i = 0; i < noOfNumbers; i++ ) {
printf("number[%d] = %d\n", i, number[i]);
}
return 0;
}
How you ensure the array of ints is big enough is left to you.
Use strtok to separate the numbers and then use atoi on each of them to convert them to integers.
http://www.elook.org/programming/c/strtok.html
int output[4];
char *input_string = "1 10 14 16";
sscanf(input_string, "%d %d %d %d", &output[0], &output[1], &output[2], &output[3]);
printf(" OUTPUT : %d %d %d %d\n", output[0], output[1], output[2], output[3]);
Above code should work
Regards
1 you will have to a strtok to get one number string at a time.
2 Then you have todo atoi to convert a number string into an int.
This needs to be done till the loop condition of strtok returns a valid string
#include <stdio.h>
///i is count *input_string[] times
///j is count temp[] times
int main() {
int i,j=0,num=0,*ptr,output;
char temp[9]={'\0'};
char *input_string[] = {"1 10 14 16 20 31 34 67 23 45 23"};
for(i=0;*(*(input_string+0)+i)!='\0';)
{
switch(*(*(input_string+0)+i))
{
case ' ':i++;break;
case '1':case '2':case '3':
case '4':case '5':case '6':
case '7':case '8':case '9':
case '0':
j=0;
while(*(*(input_string+0)+i) != ' ' || j>9 )
{
temp[j]=*(*(input_string+0)+i);
i++;j++;
}
j++;
temp[j]='\0';
output=atoi(temp);
ptr=malloc( sizeof(int) );
ptr=&output;
printf("%d ",*ptr);
break;
}
}
return 0;
}
I made small modifications.I updated the malloc (); function.May I ask whether the condition?Thank you!
#include <stdio.h>
///i is count *input_string[] times
///j is count temp[] times
///num is count output[] times
int main() {
int i,j=0,num=0;
int output[4];
char temp[9]={'\0'};
char *input_string[] = {"1 10 14 16"};
for(i=0;*(*(input_string+0)+i)!='\0';)
{
switch(*(*(input_string+0)+i))
{
case ' ':i++;break;
case '1':case '2':case '3':
case '4':case '5':case '6':
case '7':case '8':case '9':
case '0':
j=0;
while(*(*(input_string+0)+i) != ' ' || j>9 )
{
temp[j]=*(*(input_string+0)+i);
i++;j++;
}
j++;
temp[j]='\0';
output[num]=atoi(temp);
num++;
break;
}
}
for(i=0;i<4;i++)///print number
printf("%d ",output[i]);
return 0;
}
///*(*(input_string+0)+i) you can see is e.g char input_string[][];
Related
I am trying to get 4 integers from an IP-address. For example, 12.34.56.78. A = 12, b = 34, c = 56, and d = 78. Here is my code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ADDRESS[100];
printf("Enter IP: ");
scanf("%s", ADDRESS);
return 0;
}
How would I be able to do this?
try to use good old sscanf().
int A, B, C, D;
sscanf(ADDRESS, "%d.%d.%d.%d", &A, &B, &C, &D);
It may be a good idea to check if sscanf() returned 4 what indicates that all four numbers were correctly parsed.
if you're asked to read it as a string at first , you need to add a condition that it exists 4 points in the string
do {
count=0;
for (i=0;i<strlen(address);i++) {
if (address[i] == '.')
count++;
}
if (count > 4) {
printf("Enter IP: ");
scanf("%s", ADDRESS); }
while (!(count <= 4));
secondly using strtok can make everything easy , you have to split your string with the . caracter so it goes with something like this
char *tester;
int counter=0,a,b,c,d;
tester = strtok(address, ".");
while( tester != NULL ) {
switch (counter) {
case 0:
a=atoi(tester);
counter++;
break;
case 1:
b=atoi(tester);
counter++;
break;
case 2 :
c=atoi(tester);
counter++;
break;
case 3:
d=atoi(tester);
counter++;
break; }
tester= strtok(NULL, ".");
}
I need to code a program that gets input values for a string, then it ignores the characters that are not digits and it uses the digits from the string to create an integer and display it. here are some strings turned into integers as stated in the exercise.
I wanted to go through the string as through a vector, then test if the each position is a digit using isdigit(s[i]), then put these values in another vector which creates a number using the digits. At the end it's supposed to output the number. I can't for the life of it figure what's wrong, please help.
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main()
{
char *s;
scanf("%s", s);
printf("%s\n", s);
int i, n=0, v[100], nr=0;
for(i=0; i<strlen(s); i++)
{
if (isdigit(s[i]) == 1)
{
v[i] = s[i];
n++;
}
}
for(i=0;i<n;i++)
{
printf("%c\n", v[i]);
}
for(i=0; i<n; i++)
{
nr = nr * 10;
nr = nr + v[i];
}
printf("%d", nr);
return 0;
}
The pointer s is unintialized which is your major problem. But there are other problems too.
isdigit() is documented to return a non-zero return code which is not necessarily 1.
The argument to isdigit() needs to be cast to unsigned char to avoid potential undefined behaviour.
Your array v is also using the same index variable i - which is not right. Use a different variable to index v when you store the digits.
You need to subtract '0' to get the each digits integer equivalent.
scanf()'s format %s can't handle inputs with space (among other problems). So, use fgets().
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main(void)
{
char s[256];
fgets(s, sizeof s, stdin);
s[strcspn(s, "\n")] = 0; /* remove trailing newline if present */
printf("%s\n", s);
int i, n = 0, v[100], nr = 0;
size_t j = 0;
for(i = 0; i < s[i]; i++)
{
if (isdigit((unsigned char)s[i]))
{
v[j++] = s[i];
n++;
}
}
for(i = 0;i < j; i++)
{
printf("%c\n", v[i]);
}
if (j) { /* No digit was seen */
int multiply = 1;
for(i= j-1 ; i >= 0; i--) {
nr = nr + (v[i] - '0') * multiply;
multiply *= 10;
}
}
printf("%d", nr);
return 0;
}
In addition be aware of integer overflow of nr (and/or multiply) can't hold if your input contains too many digits.
Another potential source of issue is that if you input over 100 digits then it'll overflow the array v, leading to undefined behaviour.
Thanks a lot for your help, i followed someone's advice and replaced
v[i] = s[i] -> v[n] = s[i] and changed char *s with char s[100]
now it works perfectly, i got rid of the variable nr and just output the numbers without separating them through \n . Thanks for the debugger comment too, I didn't know I can use that effectively.
Firstly, you did not allocate any memory, I changed that to a fixed array.
Your use of scanf will stop at the first space (as in the first example input).
Next, you don't use the right array index when writing digits int v[]. However I have removed all that and simply used any digit that occurs.
You did not read the man page for isdigit. It does not return 1 for a digit. It returns a nonzero value so I removed the explicit test and left it as implicit for non-0 result.
I changed the string length and loop types to size_t, moving the multiple strlen calls ouside of the loop.
You have also not seen that digits' character values are not integer values, so I have subtracted '0' to make them so.
Lastly I changed the target type to unsigned since you will ignore any minus sign.
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main(void)
{
char s[100]; // allocate memory
unsigned nr = 0; // you never check a `-` sign
size_t i, len; // correct types
if(fgets(s, sizeof s, stdin) != NULL) { // scanf stops at `space` in you example
len = strlen(s); // outside of the loop
for(i=0; i<len; i++) {
if(isdigit(s[i])) { // do not test specifically == 1
nr = nr * 10;
nr = nr + s[i] - '0'; // character adjustment
}
}
printf("%u\n", nr); // unsigned
}
return 0;
}
Program session:
a2c3 8*5+=
2385
Just use this
#include <stdio.h>
#include <ctype.h>
int main()
{
int c;
while ((c = getchar()) != EOF) {
switch (c) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
case '\n':
putchar(c); break;
}
}
return 0;
} /* main */
This is a sample execution:
$ pru_$$
aspj pjsd psajf pasdjfpaojfdapsjd 2 4 1
241
1089u 0u 309u1309u98u 093u82 40981u2 982u4 09832u4901u 409u 019u019u 0u3 0ue
10890309130998093824098129824098324901409019019030
Very elegant! :)
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]);
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.
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;
}