i am using c++ win32 Api.
i want to split the character using delimiter.
that character like "CN=USERS,OU=Marketing,DC=RAM,DC=COM".
i want to split the charcter into after the first comma(,).that means i need only
OU=Marketing,DC=RAM,DC=COM.
i already tried strtok function,but it split CN=USERS only.
How can i achieve this?
Try below code, you should be able to get each item(separated by ',') easily:
strtok version:
char domain[] = "CN=USERS,OU=Marketing,DC=RAM,DC=COM";
char *token = std::strtok(domain, ",");
while (token != NULL) {
std::cout << token << '\n';
token = std::strtok(NULL, ",");
}
std::stringstream version:
std::stringstream ss("CN=USERS,OU=Marketing,DC=RAM,DC=COM");
std::string item;
while(std::getline(ss, item, ','))
{
cout << item << endl;
}
Have a look at std::getline()
http://en.cppreference.com/w/cpp/string/basic_string/getline
Using strchr makes it quite easy:
char domain[] = "CN=USERS,OU=Marketing,DC=RAM,DC=COM";
char *p = strchr(domain, ',');
if (p == NULL)
{
// error, no comma in the string
}
++p; // point to the character after the comma
Related
I used two strings one to store words and other to store the output string. The idea is to go through character by character and whenever space is encountered , push it in the newly form string after checking whether it already exists or not.
But the code is not giving any output and I am not able to find out why.
I just want to know what I am doing wrong
#include <iostream>
using namespace std;
int main() {
string str;
cin>>str;
string str1 = "";
string word = "";
for(auto x : str){
if(x == ' '){
size_t found = str1.find(word);
if(found == string::npos){
str1 += " " + word;
}
}
else
word += x;
}
cout<<str1;
}
I see three problems here:
cin >> str will only read until the next space, so you only ever process one word. Use std::getline instead:
string str;
std::getline(cin, str);
You only update str1 when you encounter a space in str, so you lose the last word unless the input ends with a space. You can simply append a space on the input before using it. For example put
str.push_back(' ');
after reading the input.
You don't clear word, so it will grow longer and longer. You can use std::string::clear() like so:
if(x == ' ') {
size_t found = str1.find(word);
if(found == string::npos){
str1 += " " + word;
}
// VVVVVVVVVV reset word buffer after word processing of word finished
word.clear();
}
This question already has answers here:
C's strtok() and read only string literals
(5 answers)
Closed 8 years ago.
I have a simple code where Iam trying to go through a char* and spit it into separate words. Here is the simple code I have.
#include <iostream>
#include <stdio.h>
int main ()
{
char * string1 = "- This is a test string";
char * character_pointer;
std::cout << "Splitting stringinto tokens:" << string1 << std::endl;
character_pointer = strtok (string1," ");
while (character_pointer != NULL)
{
printf ("%s\n", character_pointer);
character_pointer = strtok (NULL, " ");
}
return 0;
}
I am getting an error that will not allow me to do this.
So my question is, how do I go through and find each word in a char*. For my actual program I am working on, one of my libraries returns a paragraph of words as a const char* and I need to stem each word using a stemming algorithm (I know how to do this, I just do not know how to send each individual word to the stemmer). If someone could just solve how to get the example code to work, I will be able to figure it out. All of the examples online use a char[] for string1 instead of a char* and I cannot do that.
This is the simplest (codewise) way I know to split a string in c++:
std::string string1 = "- This is a test string";
std::string word;
std::istringstream iss(string1);
// by default this splits on any whitespace
while(iss >> word) {
std::cout << word << '\n';
}
or like this if you want to specify a delimiter.
while(std::getline(iss, word, ' ')) {
std::cout << word << '\n';
}
Here's a corrected version, try it out:
#include <iostream>
#include <stdio.h>
#include <cstring>
int main ()
{
char string1[] = "- This is a test string";
char * character_pointer;
std::cout << "Splitting stringinto tokens:" << string1 << std::endl;
character_pointer = strtok (string1," ");
while (character_pointer != NULL)
{
printf ("%s\n", character_pointer);
character_pointer = strtok (NULL, " ");
}
return 0;
}
There are different ways you could do this in C++.
If space is your delimited then you can get the tokens this way:
std::string text = "- This is a test string";
std::istringstream ss(text);
std::vector<std::string> tokens;
std::copy(std::istream_iterator<std::string>(ss),
std::istream_iterator<std::string>(),
std::back_inserter<std::vector<std::string>>(tokens));
You can also tokenize the string in C++ using regular expressions.
std::string text = "- This is a test string";
std::regex pattern("\\s+");
std::sregex_token_iterator it(std::begin(text), std::end(text), pattern, -1);
std::sregex_token_iterator end;
for(; it != end; ++it)
{
std::cout << it->str() << std::endl;
}
Forget about strtok. To get exactly what you seem to be
aiming for:
std::string const source = "- This is a test string";
std::vector<std::string> tokens;
std::string::const_iterator start = source.begin();
std::string::const_iterator end = source.end();
std::string::const_iterator next = std::find( start, end, ' ' );
while ( next != end ) {
tokens.push_back( std::string( start, next ) );
start = next + 1;
next = std::find( start, end, ' ' );
}
tokens.push_back( std::string( start, next ) );
Of course, this can be modified as much as you want: you can use
std::find_first_of is you want more than one separator, or
std::search if you want a multi-character separator, or even
std::find_if for an arbitrary test (with a lambda, if you have
C++11). And in most of the cases where you're parsing, you can
just pass around two iterators, rather than having to construct
a substring; you only need to construct a substring when you
want to save the extracted token somewhere.
Once you get used to using iterators and the standard
algorithms, you'll find it a lot more flexible than strtok,
and it doesn't have all of the drawbacks which the internal
state implies.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Splitting a string in C++
I'm trying to split a single string object with a delimeter into separate strings and then output individual strings.
e.g The input string is firstname,lastname-age-occupation-telephone
The '-' character is the delimeter and I need to output them separately using the string class functions only.
What would be the best way to do this? I'm having a hard time understanding .find . substr and similar functions.
Thanks!
I think string streams and getline make for easy-to-read code:
#include <string>
#include <sstream>
#include <iostream>
std::string s = "firstname,lastname-age-occupation-telephone";
std::istringstream iss(s);
for (std::string item; std::getline(iss, item, '-'); )
{
std::cout << "Found token: " << item << std::endl;
}
Here's using only string member functions:
for (std::string::size_type pos, cur = 0;
(pos = s.find('-', cur)) != s.npos || cur != s.npos; cur = pos)
{
std::cout << "Found token: " << s.substr(cur, pos - cur) << std::endl;
if (pos != s.npos) ++pos; // gobble up the delimiter
}
I'd do something like this
do
{
std::string::size_type posEnd = myString.find(delim);
//your first token is [0, posEnd). Do whatever you want with it.
//e.g. if you want to get it as a string, use
//myString.substr(0, posEnd - pos);
myString = substr(posEnd);
}while(posEnd != std::string::npos);
Let's say I have a string that has multiple carriage returns in it, i.e:
394968686
100630382
395950966
335666021
I'm still pretty amateur hour with C++, would anyone be willing to show me how you go about: parsing through each "line" in the string ? So I can do something with it later (add the desired line to a list). I'm guessing using Find("\n") in a loop?
Thanks guys.
while (!str.IsEmpty())
{
CString one_line = str.SpanExcluding(_T("\r\n"));
// do something with one_line
str = str.Right(str.GetLength() - one_line.GetLength()).TrimLeft(_T("\r\n"));
}
Blank lines will be eliminated with this code, but that's easily corrected if necessary.
You could try it using stringstream. Notice that you can overload the getline method to use any delimeter you want.
string line;
stringstream ss;
ss << yourstring;
while ( getline(ss, line, '\n') )
{
cout << line << endl;
}
Alternatively you could use the boost library's tokenizer class.
You can use stringstream class in C++.
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
int main()
{
string str = "\
394968686\
100630382\
395950966\
335666021";
stringstream ss(str);
vector<string> v;
string token;
// get line by line
while (ss >> token)
{
// insert current line into a std::vector
v.push_back(token);
// print out current line
cout << token << endl;
}
}
Output of the program above:
394968686
100630382
395950966
335666021
Note that no whitespace will be included in the parsed token, with the use of operator>>. Please refer to comments below.
If your string is stored in a c-style char* or std::string then you can simply search for \n.
std::string s;
size_t pos = s.find('\n');
You can use string::substr() to get the substring and store it in a list. Pseudo code,
std::string s = " .... ";
for(size_t pos, begin = 0;
string::npos != (pos = s.find('\n'));
begin = ++ pos)
{
list.push_back(s.substr(begin, pos));
}
I have a string "stack+ovrflow*newyork;" i have to split this stack,overflow,newyork
any idea??
First and foremost if available, I would always use boost::tokenizer for this kind of task (see and upvote the great answers below)
Without access to boost, you have a couple of options:
You can use C++ std::strings and parse them using a stringstream and getline (safest way)
std::string str = "stack+overflow*newyork;";
std::istringstream stream(str);
std::string tok1;
std::string tok2;
std::string tok3;
std::getline(stream, tok1, '+');
std::getline(stream, tok2, '*');
std::getline(stream, tok3, ';');
std::cout << tok1 << "," << tok2 << "," << tok3 << std::endl
Or you can use one of the strtok family of functions (see Naveen's answer for the unicode agnostic version; see xtofls comments below for warnings about thread safety), if you are comfortable with char pointers
char str[30];
strncpy(str, "stack+overflow*newyork;", 30);
// point to the delimeters
char* result1 = strtok(str, "+");
char* result2 = strtok(str, "*");
char* result3 = strtok(str, ";");
// replace these with commas
if (result1 != NULL)
{
*result1 = ',';
}
if (result2 != NULL)
{
*result2 = ',';
}
// output the result
printf(str);
Boost tokenizer
Simple like this:
#include <boost/tokenizer.hpp>
#include <vector>
#include <string>
std::string stringToTokenize= "stack+ovrflow*newyork;";
boost::char_separator<char> sep("+*;");
boost::tokenizer< boost::char_separator<char> > tok(stringToTokenize, sep);
std::vector<std::string> vectorWithTokenizedStrings;
vectorWithTokenizedStrings.assign(tok.begin(), tok.end());
Now vectorWithTokenizedStrings has the tokens you are looking for. Notice the boost::char_separator variable. It holds the separators between the tokens.
See boost tokenizer here.
You can use _tcstok to tokenize the string based on a delimiter.
This site has a string tokenising function that takes a string of characters to use as delimiters and returns a vector of strings.
Simple STL String Tokenizer Function
There is another way to split a string using c/c++ :
First define a function to split a string:
//pointers of the substrings, assume the number of fields will not be over 5
char *fields[5];
//str: the string to splitted
//splitter: the split charactor
//return the real number of fields or 0 if any error exits
int split(char* str, char *splitter)
{
if(NULL == str)
{
return 0;
}
int cnt;
fields[0] = str;
for(cnt = 1; (fields[cnt] = strstr(fields[cnt - 1], splitter)) != NULL &&
cnt < 5; cnt++)
{
*fields[cnt] = '\0';
++fields[cnt];
}
return cnt;
}
then you can use this function to split string as following:
char* str = "stack+ovrflow*newyork;"
split(str, "+");
printf("%s\n", fields[0]); //print "stack"
split(fields[1], "*");
printf("%s\n", fields[0]); //print "ovrflow"
split(fields[1], ";");
printf("%s\n", fields[0]); //print "newyork"
this way will be more efficient and reusable