I have a problem with strtok() - it does not return the input as expected.
void parse_input(const char *input,unsigned char *ctext, int mlen){
char * str = strdup(input);
char * pch = strtok(str,"-");
while (pch != NULL)
{
ctext[mlen] = (int) pch;
pch = strtok (NULL, "-");
mlen++;
}
On input like 1-2-3-4 I would want it to fill ctext with [1,2,3,4].
That doesn't work, however.
What am I doing wrong? Any help appreciated.
ctext[mlen] = (int) pch;
That stores the numeric value of the pointer, whereas you really want the character pointed to by the pointer. Time to read a good article/book/tutorial on pointers.
ctext[mlen] = *pch;
is what you're looking for.
You want to get the character in the first byte of pch -- not the address of pch
ctext[mlen] = *pch;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void parse_input(const char *input,unsigned char *ctext[], int *mlen){
char * str = strdup(input);
char * pch = strtok(str,"-");
while (pch != NULL){
ctext[(*mlen)++] = (unsigned char*)pch;
pch = strtok (NULL, "-");
}
}
int main(void){
unsigned char *ctext[16];
int mlen=0;
int i;
parse_input("1-2-3-4", ctext, &mlen);
printf("[ ");
for(i=0;i<mlen;++i){
printf("%s", ctext[i]);
if(i<mlen -1)
printf(", ");
}
printf(" ]\n");
//free(ctext[0]);
return 0;
}
Related
Can someone please help me with this problem, I want my program to print + with every end of line.
My code is:
#include <stdio.h>
#include <string.h>
int main()
{
char str[65];
char * pch;
char str1[65];
fgets (str, 100, stdin);
pch = strtok (str," ");
while (pch != NULL)
{
str1=pch;
printf("=");
printf ("%s\n",pch);
pch = strtok (NULL, " ");
}
return 0;
}
With input, "Muhannad Stack" (output wrong):
=Muhannad
=Stack
Correct:
=Muhannad+
=Stack+
Therefore, my problem is how to add + at the end of every printed line
Here is what would work:
#include <stdio.h>
#include <string.h>
int main()
{
char str[65];
char * pch;
char str1[65];
fgets (str, 100, stdin);
pch = strtok (str,"\n");
pch = strtok (pch," ");
while (pch != NULL) {
printf ("=%s+\n",pch);
pch = strtok (NULL, " ");
}
return 0;
}
I have this code:
char *pch;
pch = strtok(texto," ");
while (pch != NULL)
{
pch = strtok (NULL, " ");
}
My "texto" variable have something like "This is my text example".
And i need to store each value comming from pch in while, in an array of characteres, but i really dont know how to do it.
I need something like that, each value in array will have a word, so:
"This is my text example".
Array[0][20] = This;
Array[1][20] = is;
Array[2][20] = my;
Array[3][20] = text;
Array[4][20] = example;
The pch in while having all these words already split, but I don't know how to add into a char array, or how I will declare him too.
Consider the following example:
#include <stdio.h>
#include <string.h>
#define MAXSTRLEN 20
#define MAXWORD 6
int main(void)
{
char arr[MAXWORD][MAXSTRLEN+1] = {0};
char str[] ="This is my text example";
char *pch;
int i = 0;
pch = strtok (str," ");
while (pch != NULL && i < MAXWORD)
{
strncpy(arr[i++], pch, MAXSTRLEN);
pch = strtok (NULL, " ");
}
return 0;
}
This should work. Use strcpy to copy pch to character array.
char str[] ="This is my text example";
char *pch;
int i = 0;
pch = strtok (str," ");
char a[10][20];
while (pch != NULL)
{
strcpy(a[i++], pch);
pch = strtok (NULL, " ");
}
And as #stackptr has suggested dont use strtok and instead use strtok_r . For more info on this.
I have read through countless strtok posts, even copied some directly in their entirety into a new int main, but I can't figure out how to create the functions get_first and get_second.
get_first("This is a sentence."); //returns "This"
get_rest("This is a sentence."); //returns "is"
This is what I have so far, I have had nothing but trouble with strtok, but I don't know what else to use.
#include <stdio.h>
#include <string.h>
char * get_first(char * string) {
string = strtok(string, " ");
return string;
}
char * get_second(char * string) {
string = strtok(string, " ");
string = strtok(NULL, " ");
return string;
}
int main(int argc, char * argv[]) {
char * test_string = "This is a sentence.";
char * first = get_first(test_string);
char * second = get_second(test_string);
printf("%s\n", first);
printf("%s\n", second);
}
Getting no faults compiling with gcc -g -Wall, but it always seg faults. I think I have tried every permutation of char c[] and char * c there is.
strtok changes the string. (but String literals are not allowed to change.)
So create a copy.
Do the following:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char * get_first(const char *string){
char *clone = strdup(string);//create copy, strdup is non standard. malloc and copy.
char *token = strtok(clone, " ");
if(token)
token = strdup(token);
free(clone);
return token;
}
char * get_second(const char *string) {
char *clone = strdup(string);
char *token = strtok(clone, " ");
if(token && (token = strtok(NULL, " ")))
token = strdup(token);
free(clone);
return token;
}
int main(void) {
char * test_string = "This is a sentence.";
char * first = get_first(test_string);
char * second = get_second(test_string);
printf("%s\n", first);
printf("%s\n", second);
free(first);
free(second);
}
I get a segmentation fault the second time malloc runs:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int keywords_init(char *str, char ***str_arr);
int main(void) {
char str[] = "keyword1,keyword2,keyword3";
char **str_arr = NULL;
int arr_elements;
arr_elements = keywords_init(str, &str_arr);
return 0;
}
int keywords_init(char *str, char ***str_arr) {
int i;
char *pch;
/* String break */
pch = strtok(str, ",");
for (i = 0; pch != NULL; i++) {
*str_arr = realloc (*str_arr, (i+1)*sizeof(char *));
*str_arr[i] = malloc (strlen(pch) + 1);
strcpy(*str_arr[i], pch);
printf("%d: %s\n", i, pch);
pch = strtok (NULL, ",");
}
return i;
}
What confuses me is that if I don't pass the address of str_arr to keywords_init and use a double pointer instead of a triple one in keywords_init it works just fine.
You're getting bitten by operator precedence/associativity - change both occurrences of:
*str_arr[i]
to:
(*str_arr)[i]
Hey I'm having problems with my code I get creating the tokens and have it adding it add the tokens to a 2d array but it doesn't work correctly. Any idea why.
/* strtok example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] ="This a sample string";
char * st[4][0];
char * pch;
int i;
printf ("Splitting string \"%s\" into tokens:\n",str);
pch = strtok (str," ");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ");
for(i=0;i<4;i++)
{
st[i][0]=pch;
}
}
print(st, i);
return 0;
}
void print(char st[4][0], int i)
{
for(i=0;i<4;i++)
{
printf("%d - %s",i ,st[i][0]);
}
}
char * st[4][0];
You are allocating an array of zero length. later you try to access the first element, which is non-existent, and therefore you get undefined behaviour.
I cannot see why this array has two dimensions anyway. You only access the first element of the second dimension, why not:
char * st[4];
??
To be more precise I don't understand the usage of this variable at all. Why do you write the same value in all four elements?
There are a number of problems: compare with this code:
/* strtok example */
#include <stdio.h>
#include <string.h>
void print(char *st[4]) // Fixed parameter type
{
int i; // i is a local counter
for(i=0;i<4;i++)
{
printf("%d - %s\n",i ,st[i]);
}
}
int main ()
{
char str[] ="This a sample string";
char * st[4]; // Corrected array definition
char * pch;
int i=0; // Initialise counter i to 0
printf ("Splitting string \"%s\" into tokens:\n",str);
pch = strtok (str," ");
while (pch != NULL)
{
st[i]=pch; // Store string before overwriting pch, and only store in a single location
printf ("%s\n",pch);
pch = strtok (NULL, " ");
i++; // increment i inside loop
}
print(st);
return 0;
}