strtok

What are the differences between strtok and strsep in C

狂风中的少年 提交于 2019-12-17 04:32:13
问题 Could someone explain me what differences there are between strtok() and strsep() ? What are the advantages and disadvantages of them? And why would I pick one over the other one. 回答1: From The GNU C Library manual - Finding Tokens in a String: One difference between strsep and strtok_r is that if the input string contains more than one character from delimiter in a row strsep returns an empty string for each pair of characters from delimiter. This means that a program normally should test

Using strtok with a std::string

一个人想着一个人 提交于 2019-12-17 02:28:36
问题 I have a string that I would like to tokenize. But the C strtok() function requires my string to be a char* . How can I do this simply? I tried: token = strtok(str.c_str(), " "); which fails because it turns it into a const char* , not a char* 回答1: #include <iostream> #include <string> #include <sstream> int main(){ std::string myText("some-text-to-tokenize"); std::istringstream iss(myText); std::string token; while (std::getline(iss, token, '-')) { std::cout << token << std::endl; } return 0

Parse string into array based on spaces or “double quotes strings”

霸气de小男生 提交于 2019-12-14 04:18:53
问题 Im trying to take a user input string and parse is into an array called char *entire_line[100]; where each word is put at a different index of the array but if a part of the string is encapsulated by a quote, that should be put in a single index. So if I have char buffer[1024]={0,}; fgets(buffer, 1024, stdin); example input: "word filename.txt "this is a string that shoudl take up one index in an output array"; tokenizer=strtok(buffer," ");//break up by spaces do{ if(strchr(tokenizer,'"')){/

Doubt regarding Strtok in PHP

橙三吉。 提交于 2019-12-13 20:24:01
问题 <?php $string = "sandesh commented on institue international institute of technology"; /* Use tab and newline as tokenizing characters as well */ $tok = strtok($string, " \n\t"); echo $tok ?> The above code gives me output sandesh. But if I want the output "commented on institute international institute of technology", then how should I modify the above code. Thanks and Regards Sandesh 回答1: <?php $string = "sandesh commented on institue international institute of technology"; echo substr(

strtok not working as expected

喜欢而已 提交于 2019-12-13 11:29:06
问题 I tried to write a function that gets a pointer to array of char, reads a string from the user and remove all spaces in the start of the string till the first char that is not string appear. Finally return the copy of the string without space/s in the begining. For example, for input abcd the function should return pointer to the string abcd . for input 123 123 the function should return pointer to string 123 123 . The function is shown below, void read_RemoveSpace(char * str)/**read the rest

Removing the first token within a char array and keeping the rest in C

我们两清 提交于 2019-12-13 06:23:40
问题 So if I have the following char array in C: "a b c" // where "a", "b", and "c" can be char arrays of any length and the // space between them can be of any length How can I remove the "a" token but store the rest "b c" in an char pointer? So far I have implemented the following method that doesn't work: char* removeAFromABC(char* a, char* abc) { char* abcWithoutA[MAXIMUM_LINE_LENGTH + 1]; int numberOfCharsInA = strlen(a); strcpy(abcWithoutA, (abc + numberOfCharsInA)); return abcWithoutA; }

Extracting the extension of a file

本小妞迷上赌 提交于 2019-12-13 05:37:09
问题 Some details Language: C System: Linux; working with command line (terminal), files are read in through the terminal User experience with C: 3 months I have been trying to extract the extension of a given file for example "myfile.wld", so that later I can make a check to see if the right type of file has been entered at the terminal before I work on the contents of the file. This is necessary for an assignment I have used the function "strtok" to separate the input into sections by a

Why is my strcmp() failing?

北战南征 提交于 2019-12-13 05:25:55
问题 I am a C newbie and learning string tokenizing. I am trying to compare two strings in the following way. But the string comparison I am doing is failing. Can you please let me know what I am missing here? I couldn't find another similar question, may be due to my inexperience in C. If one exists, can you please redirect me to it? char* input = "comparer here"; char* args[5]; int counter = 0; char *tok = strtok(input, " "); while (tok != NULL) { args[counter] = tok; counter ++; if (counter ==

Parsing command line statements as a list of tokens

China☆狼群 提交于 2019-12-13 01:27:35
问题 #include <stdio.h> #include <string.h> /* needed for strtok */ #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { char text[10000]; fgets(text, sizeof(text), stdin); char *t; int i; t = strtok(text, "\"\'| "); for (i=0; t != NULL; i++) { printf("token %d is \"%s\"\n", i, t); t = strtok(NULL, "\"\'| "); } } This is part of the code that im trying to make it is supposed to separate tokens Let's say the input is 'abc' "de f'g" hij| k "lm | no" The output should be token 1:

Copy results of strtok to 2 strings in C

纵然是瞬间 提交于 2019-12-12 23:23:11
问题 Ok, so I have the code char *token; char *delimiter = " "; token = strtok(command, delimiter); strcpy(command, token); token = strtok(NULL, delimiter); strcpy(arguments, token); and it gives me EXC_BAD_ACCESS when i run it, and yes, command and arguments are already defined. 回答1: Why are you copying the token into command when you're parsing command ? It's a very unsafe thing to do. You can do: char *command_tok, *args_tok; command_tok = strtok(command, delimiter); args_tok = strtok(NULL,