I have started learning c. Currently, I am trying to get words out of two char arrays so that I can compare them using my helper method called compare. However, my strtok() is giving me weird output.
Heres my code:
char *headerPointer=headers;
char *linePointer=firstline;
printf("Header-%s\n",headers);
printf("Line-%s\n",firstline);
headerPointer=strtok(headerPointer,",");
linePointer=strtok(linePointer,",");
while ((headerPointer!=NULL&&linePointer!=NULL)) {
printf("\nPrinting words from headers\n");
printf("%s",headerPointer);
headerPointer=strtok(NULL,",");
printf("\nPrinting words from line\n");
printf("%s",linePointer);
linePointer=strtok(NULL, ",");
}
Output produced by above code:
Header-Hello,My,name,is,Ram.
Line-I,own,20,thousand,bucks.
Printing words from headers-
Hello.
Printing words from line-
I.
Printing words from headers-
own.
Printing words from line-
20.
Printing words from headers-
thousand.
Printing words from line-
bucks.
I don't understand why header is printing the contents from line and where does my,name,is,ram go?
I tried to write them using the following code and this same code produced the desired output.
Different Code Style:
char *headerPointer=headers;
char *linePointer=firstline;
printf("Header%s\n",headers);
printf("Line%s\n",firstline);
headerPointer=strtok(headerPointer,",");
while(headerPointer!=NULL)
{
printf("\nPrinting words from headers\n");
printf("%s",headerPointer);
headerPointer=strtok(NULL,",");
}
linePointer=strtok(linePointer,",");
while(linePointer!=NULL){
printf("\nPrinting words from line\n");
printf("%s",linePointer);
linePointer=strtok(NULL, ",");
}
Output:
Header-Hello,My,name,is,Ram.
Line-I,own,20,thousand,bucks.
Printing words from headers-
Hello.
Printing words from headers-
My.
Printing words from headers-
name.
Printing words from headers-
is.
Printing words from headers-
Ram.
Printing words from line-
I.
Printing words from line-
own.
Printing words from line-
20.
Printing words from line-
thousand.
Printing words from line-
bucks.
Please explain why two codes based on a same idea are producing different results? Can we modify the first code to give results like second? I tried to search and followed the solution already available but couldn't get far.
strtok is stateful and non-reentrant; it can only tokenize one string at a time. In your original code, you are trying to tokenize along two inputs at once, but it can't do that; it's only tokenizing along the last non-NULL string argument provided, which was linePointer.
To make this work, you need to use strtok_r which allows you to save the progress on each string without overwriting the progress on the other string.
Related
I was wondering if it's possible to read from a 3M lined file word by word, and save the words of lines 1-1M in array1 (lines 1M-2M to array2, etc) to 3 arrays of strings. The problem is when reading word by word you cannot count the lines.
Obviously, one could try to read line by line and tokenize the words from the line, but it seems to be too much work. Is there a better way?
when I am trying to concatenate a word and a sentence the first word of the sentence automatically deleted, what improvement should I use.. my code is: enter code here`
char word[]="AddMe";
char adder[150];
// reading string from stdin
scanf("%[^\n]*c",%sentence);
strcat(adder,word);
strcat(adder,sentence);
puts(adder);
First, use:
strcpy(adder,word);
The format of the last line as seen below completely random. The first part of the program has experiment names inputted into *experiments[20] while data for each experiment is put into data[10][20]. After a certain line in the input redirection where "*** END ***" is read, the data input is terminated. The following line is of our options.They do this:1. Show all data. 2. Calculate the average for a specific experiment (therefore the name of the experiment HAS to follow 2 in the file. 3. Calculate the total average of all experiments. 4. End the program. Everything needs to be done through file redirection input
Main Question: How do i tokenize a string composed of two words, as you can see two lines below?
Ok, so we have stdin file redirection input of this last line of a file:
1 2 Control Group 3 4
There are 4 possible options: 1,2,3,4
2 is always followed by the name of an experiment as it calculates the average of that specific experiment.
We tokenize the line obtained through fgets() by doing this:
token = strtok(str," ");
and then by continuing like this for other integers:
token = strtok(NULL," ");
Each token of a number is scanned into an int var as such:
sscanf (token, "%d", &var);
When var is equal to 2, the switch statement creates a new token, expecting a String to follow. Originally I had written the code as such:
printf("What experiment would you like to use?\n");
token = strtok (NULL," ");
sscanf (token, "%s", &str);
And then I would compare str with my different experiment names in a loop using strcmp. However I only tested it with 1 word experiment names. Now I'm realizing the problem can be written as listed at the beginning of my question.
Is there a simple solution to this?
Thanks.
I am parsing a text file, and when I come across the word .word, I want to grab the rest of the line. Here is what I have so far:
char *word_ptr;
if (strstr(token, ":")){
// Some code
}
else if ((word_ptr = strstr(token, ".word"))) {
char *string_wanted = word_ptr + 6;
printf("Rest: '%s'\n", string_wanted);
}
string_wanted is not printing correctly. Is my usage of word_ptr correct when assigning it in the else-if statement? string_wanted is printing out nothing. When I add a 7 instead of a 6, it prints out 'ize'. I had the word size in my text file but now I removed it, I deleted the file and re-created it and done a clean build and the word 'ize' still shows up!! It does not exist in the file at all anymore so where did it come from?? I am really frustrated the word 'size' does not exist anymore in the file.
Here is what the file looked (when I had the word 'ize'):
array: .word 0:10
array_size: .word 10
Now I just removed the second line, so it is:
array: .word 0:10
Why isn't word_ptr printing out 0:10 when I add a 6 to it? I am pretty sure that word_ptr points to .word because when I print it, it prints .word. When tokenizing the line, array: is being tokenized so I know that it is getting there.
Any suggestions?
Thanks for your help.
We're running around in circles a bit here. I think I see where your confusion is now, so I'll try to lay it out. Your tokenizer is taking a string and breaking it up into tokens. Each of these tokens is a separate string by itself. You don't specify what characters you are tokenizing on, so I'll just assume the space character.
In this case the string:
array: .word 0:10
becomes three new strings:
"array:"
".word"
"0:10"
If you are looping on your tokens (it appears that you are) then first time through the loop token will be "array:", the second time it will be ".word" and the third time it will be "0:10".
This evaluation:
word_ptr = strstr(token, ".word")
will only find ".word" during the iteration in which token contains ".word". When you then increment word_ptr by 6 chars you have moved past the end of token into undefined memory. Yes, "0:10" appears two characters after ".word" in your original string, but we are not looking at the original string in your call to strstr. We are looking just at token and token only contains ".word".
This is why it's failing. How to fix it depends somewhat on the rest of your implementation.
When you set *string_wanted to word_ptr + 6, you are pointing string_wanted at the null that terminates the string. So when you try to print *string_wanted you get an empty string. Try getting the next token instead.
Wouldn't your if (strstr(token, ":")) be true for all of your strings? It wouldn't ever get to the block of code you're asking about.
writing another program, it reads a txt file, and stores all the letter characters and spaces (as \0) in a char array, and ignores everything else. this part works.
now what i need it to do is read a user inputted string, and search for that string in the array, then print the word every time it appears. im terrible at I/O in C, how do you read a string then find it in a char array?
#include <stdio.h>
...
char str [80];
printf ("Enter your word: ");
scanf ("%s",str);
char* pch=strstr(fileData,str);
while (pch!=NULL)
{
printf ("found at %d\n",pch-fileData+1);
pch=strstr(pch+1,str);
}
read in the user inputted string as a char array as well (cause strings are basically char* anyway in C)
use a string matching algorithm like Boyer-Moore or Knutt-Morris-Pratt (more popularly known as KMP) - google for it if you like for C implementations of these - cause they're neat, tried and tested ways of searching strings for substrings and pattern matches and all.
for each of these indexOf cases, print the position where the word is found maybe? or if you prefer, the number of occurrences.
Generally, the list of C string functions, found here, say, are of the format str* or strn*, depending on requirements.
One for-loop inside another for-loop (called nested loop). Go through all the letters in your array, and for each letter go through all the letters in your input string and find out if that part of the array matches with the input string. If it does, print it.