Cut string in words with ------- as delimiter [duplicate] - c++

This question already has answers here:
Parse (split) a string in C++ using string delimiter (standard C++)
(33 answers)
Closed 1 year ago.
Hello i have the following sentence which is in a string and in one Line but very long:
l-s-s---s-l---s-s-s-------s-l---l---l-s-s-s-s-l---l-l-s-s---s---s-s---l-s-l-s---s-s-s-s---s---l-s-------l-s-l-l-s---s-l-l-s-l-s---l-s-l-l-s-l-------
(Its in Morse Code) The --- (3x-) seperates the letters and ------- (7x-) seperates the word.
How can i cut the very long code in words.
I've tried the following:
size_t posWordNext{};
size_t posWordPre{};
while (true) {
posWordNext += code.find("-------");
if (posWordNext >= code.size()) {
break;
}
cout << code.substr(posWordPre, posWordNext) << endl;
posWordPre = posWordNext;
}
This is the output:
l-s-s---s-l---s-s-s
-------s-l---l---l-s-s-s-s-l---l-l-s-s
s-s-s-s-l---l-l-s-s---s---s-s---l-s-l-s---s-s-s-s---s---l
---s---s-s---l-s-l-s---s-s-s-s---s---l-s-------l-s-l-l-s---s-l-l-s-l-s---l-s

Every time you call code.find(), you are searching from the beginning of the string again. You are not modifying code on each iteration, so find() will return the same offset each time. You should be passing posWordPre to the 2nd parameter of find() as a starting offset to begin searching from.
Also, when you are calling code.substr(), you are treating the 2nd parameter as an offset to stop at, but that parameter actually expects a character count instead of an offset.
Because of these mistakes, you are chopping up the code string at the wrong offsets.
Try something more like this instead:
size_t posWordPre = 0, posWordNext;
while ((posWordNext = code.find("-------", posWordPre)) != string::npos) {
cout << code.substr(posWordPre, posWordNext - posWordPre) << endl;
posWordPre = posWordNext + 7;
}
if (posWordPre < code.size())
cout << code.substr(posWordPre) << endl;
Online Demo

Related

Proper implementation for string find() function?

I am trying to push doubles to a stack given a string from stdin and until EOF. The string can be composed of doubles, ints, chars, and single spaces.
Currently, I'm utilizing the substring and find() function to account for the whitespace. It works most of the time, but for various input in which a single int is being read (exhibited below), the find() function appears to be clobbering any trailing char.
I've tried to use a variety of the different string functions to try and re-implement the way that I parse the input -- none of which has been successful.
while(std::getline(std::cin, string, '\n')){
for(unsigned int x = 0; x < string.size(); x++){
std::cout << "You read " << string[x] << std::endl;
if(isdigit(string[x])){
do{
// Get the number, stopping at the first instance of ws
std::string get_str = string.substr(x, string.find(' '));
std::cout << "You're converting " << get_str << std::endl;
// Convert it to a double
double num = stod(get_str);
std::cout << "You pushed " << number << std::endl;
// Push it to the stack
stack.push(number);
// Get the new increment
std::cout << "The size is " << get_str.size() << std::endl;
x+= get_str.size();
} while(string[x] >= '0' && string[x] <= '9');
}
/* else, do other things... */
Given an input of
100 200 + 2 /
The output is:
You read 1
You're converting 100
You pushed 100
The size is 3
You read 2
You're converting 200
You pushed 200
The size is 3
You read +
You read
You read 2
You're converting 2 /
You pushed 2
The size is 3
Specifically, I am wondering why the 3rd to last line 'You're converting 2 / ' includes the '/' when I had utilized string.find(' ') in my code as a delimiter. And given this issue, how would I be able to fix it so that only 2 is 'converted'?
Any assistance and feedback is appreciated!
The one parameter find will start the search at the beginning of the string, and return an index of the matched character. The second parameter to substr is a count of characters. Put those together with your input and you get a substring with 3 characters.

Why does my function not switch the first character with the last one of my string?

I picked up a challenge on r/dailyprogrammer on reddit which wants me to match a necklace and put the last letter at the beginning of a string. I've considered using nested for loops for this but this has made me really confused.
Instead I chose the way of replacing the last with the first character in an if-statement. But I am not getting my desired output with it, though I've tried everything what comes into my mind.
I used even std::swap() which didn't lead me to success either.
Here's the code:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
string same_necklace(string& sInput, string& sOutput)
{
for (string::size_type i = 0; i < sInput.size(); i++)
{
if (sInput[i] == sInput.size())
{
sInput[0] = sInput[sInput.size()];
}
}
for (string::size_type j = 0; j < sOutput.size(); j++)
{
if (sOutput[j] == sOutput.size() - 1)
{
sOutput[0] = sOutput[sOutput.size()];
}
}
return sInput, sOutput;
}
int main()
{
system("color 2");
string sName{ "" };
string sExpectedOutput{ "" };
cout << "Enter a name: ";
cin >> sName;
cout << "Enter expected output: ";
cin >> sExpectedOutput;
cout << "Result: " << same_necklace(sName , sExpectedOutput) << endl;
return 0;
}
And of course the link to my challenge (don't worry, it's just Reddit!):
https://www.reddit.com/r/dailyprogrammer/comments/ffxabb/20200309_challenge_383_easy_necklace_matching/
While I am waiting (hopefully) for a nice response, I will keep on trying to solve my problem.
In your if you compare the value of the current index (inside the loop) with the size of the string. Those are two unrelated things.
Also, you use a loop though you only want to do something on a single, previously known index.
for (string::size_type i = 0; i < sInput.size(); i++)
{
if (sInput[i] == sInput.size())
{
sInput[0] = sInput[sInput.size()];
}
}
You could change the if condition like this to achieve your goal:
if (i == sInput.size()-1) /* size as the index is one too high to be legal */
But what is sufficient and more elegant is to drop the if and the loop. completely
/* no loop for (string::size_type i = 0; i < sInput.size(); i++)
{ */
/* no if (sInput[i] == sInput.size())
{*/
sInput[0] = sInput[sInput.size()-1]; /* fix the index*/
/* }
} */
I.e.
sInput[0] = sInput[sInput.size()-1]; /* fix the index*/
Same for he output, though you got the correct index already correct there.
This is not intended to solve the challenge which you linked externally,
if you want that you need to describe the challenge completely and directly here.
I.e. this only fixes your code, according to the desription you provide here in the body of your question,
"put the last letter at the beginning of a string".
It does not "switch" or swap first and last. If you want that please find the code you recently wrote (surely, during your quest for learning programming) which swaps the value of two variables. Adapt that code to the two indexes (first and last, 0 and size-1) and it will do the swapping.
So much for the loops and ifs, but there is more wrong in your code.
This
return sInput, sOutput;
does not do what you expect. Read up on the , operator, the comma-operator.
Its result is the second of the two expressions, while the first one is only valuated for side effects.
This means that this
cout << "Result: " << same_necklace(sName , sExpectedOutput) << endl;
will only output the modified sExpectedOutput.
If you want to output both, the modified input and the modified output, then you can simply
cout << "Result: " << sName << " " << sExpectedOutput << endl;
because both have been given as reference to the function and hence both contain the changes the function made.
This also might not answer the challenge, but it explains your misunderstandings and you will be able to adapt to the challenge now.
You have not understand the problem i guess.
Here you need to compare two strings that can be made from neckless characters.
Lets say you have neckless four latters word is nose.
Combination is possible
1)nose
2)osen
3)seno
4)enos
your function (same_necklace) should be able to tell that these strings are belongs to same necklace
if you give any two strings as inputs to your function same_necklace
your function should return true.
if you give one input string from above group and second input string from other random word thats not belongs to above group, your function should return false.
In that sense, you just take your first string as neckless string and compare other string with all possible combination of first string.
just move move you first latter of first input string to end and then compare each resulting string to second input string.
below is the function which you can use
void swap_character(string &test)
{
int length = test.length();
test.insert(length, 1, test[0]);
test.erase(0, 1);
}

How to you get the address of an element in an std::string in C++? [duplicate]

This question already has answers here:
cout << with char* argument prints string, not pointer value
(6 answers)
Closed 5 years ago.
For example, i have the following string :
std::string s = "Hello, World!"
I want the address of the last element of s which is '!'.
I tried the following code, but it does not seem to output what I want it to.
std::cout << &s[s.length() - 1];
That outputs '!' not it's address, the same happens with s.back()
Is it because of the formatting caused by std::cout or is the problem elsewhere?
Basically I want a function that outputs the address of the last (and if possible, the first) element in a string.
Currently, you're using the overload of operator<< that takes a const char* as input. It treats the input as a null-terminated C string.
If you cast to (const void*) the problem will go away:
std::cout << (const void*)(&s[s.length() - 1]);
auto address_back = &s.back();
auto address_front = &s.front();
cout << static_cast<void*>(address_back) << endl;
cout << static_cast<void*>(address_front) << endl;

Iterating through characters of a string stored in a vector to replace characters C++

So we got an optional assignment in our C++ class. The assignment is basically this:
Write a program that holds a string of at least 8 words.
Do the following:
1. Replace the letters of first word with '?'
2. Turn the letters of the last word to uppercase
We did not yet study vectors in our class.
When I first read the assignment, storing the strings to a vector seemed like a good idea so I went with it.
To replace the characters with a '?' I used a for loop. I know that this would not work if I only had to change only certain characters or every other character to a '?'.
My issue is with converting chars of a string to uppercase.
My thought process was: for loop iterates through all chars in the last word, if the char is lowercase it gets turned to uppercase, if it is already uppercase it does not change.
I believe that my approach could work for this problem, I just maybe did not express myself correctly or I made a silly error somewhere. Could anyone assist me or push me in the right direction?
What other options are there to iterate through all chars of a string stored in a vector? Is there another approach that might work better for this? Thank you for your time.
#include <vector>
#include <cctype>
#include <string>
#include <iostream>
using namespace std;
vector<string>words;
//stores words to vector words
void storeWords()
{
cout << "Input 8 words: " << endl;
string s = " ";
for(int i=0; i<=7; i++)
{
cin >> s;
words.push_back(s);
}
}
//prints our words
void printWords()
{
cout << "\n Words stored in vector: " << endl;
for (const string s : words)
cout << s << endl;
}
//replaces chars of the first word with a '?' sign
void replace1(vector<string>&v)
{
cout << "\nReplaced characters of the first word " << words[0] << " with '?'" << endl;
for (char c : words[0])
cout << "?";
}
void replace2(vector<string>&v)
{
for (char c : words[7])
{
if(islower(c))
c = toupper(c);
}
cout << endl;
cout << words[7]<<endl;
}
int main()
{
storeWords();
printWords();
replace1(words);
replace2(words);
return 0;
}
c = toupper(c) will assign c the uppercase value. However, changing c will not change what's inside words[7]. You can get around this by referencing the char directly (char &c).
void replace2(vector<string>&v)
{
for (char &c : words[7]) {
c = toUpper(c);
}
cout << endl;
cout << words[7]<<endl;
}
Also note that your requirements are to hold a string of at least 8 words. So words[7] will probably end up looking like words[words.size() - 1].
This
for (char c : words[7])
Should be:
for (char& c : words[7])
The first version modifies a local variable, while the second changes the actual characters in words[7].
The little ampersand (&) makes c a reference to a certain character in words[7], allowing you to change c as you would words[7][some_i].
Also, I should add that your replace functions do not need that vector argument.
Ideone Example with that change

How to convert vector to string and convert back to vector

----------------- EDIT -----------------------
Based on juanchopanza's comment : I edit the title
Based on jrok's comment : I'm using ofstream to write, and ifstream to read.
I'm writing 2 programs, first program do the following tasks :
Has a vector of integers
convert it into array of string
write it in a file
The code of the first program :
vector<int> v = {10, 200, 3000, 40000};
int i;
stringstream sw;
string stringword;
cout << "Original vector = ";
for (i=0;i<v.size();i++)
{
cout << v.at(i) << " " ;
}
cout << endl;
for (i=0;i<v.size();i++)
{
sw << v[i];
}
stringword = sw.str();
cout << "Vector in array of string : "<< stringword << endl;
ofstream myfile;
myfile.open ("writtentext");
myfile << stringword;
myfile.close();
The output of the first program :
Original vector : 10 200 3000 40000
Vector in string : 10200300040000
Writing to File .....
second program will do the following tasks :
read the file
convert the array of string back into original vector
----------------- EDIT -----------------------
Now the writing and reading is fine, thanks to Shark and Jrok,I am using a comma as a separator. The output of first program :
Vector in string : 10,200,3000,40000,
Then I wrote the rest of 2nd program :
string stringword;
ifstream myfile;
myfile.open ("writtentext");
getline (myfile,stringword);
cout << "Read From File = " << stringword << endl;
cout << "Convert back to vector = " ;
for (int i=0;i<stringword.length();i++)
{
if (stringword.find(','))
{
int value;
istringstream (stringword) >> value;
v.push_back(value);
stringword.erase(0, stringword.find(','));
}
}
for (int j=0;j<v.size();i++)
{
cout << v.at(i) << " " ;
}
But it can only convert and push back the first element, the rest is erased. Here is the output :
Read From File = 10,200,3000,40000,
Convert back to vector = 10
What did I do wrong? Thanks
The easiest thing would be to insert a space character as a separator when you're writing, as that's the default separator for operator>>
sw << v[i] << ' ';
Now you can read back into an int variable directly, formatted stream input will do the conversion for you automatically. Use vector's push_back method to add values to it as you go.
Yes, this question is over a year old, and probably completely irrelevant to the original asker, but Google led me here so it might lead others here too.
When posting, please post a complete minimal working example, having to add #include and main and stuff is time better spent helping. It's also important because of your very problem.
Why your second code isn't working is all in this block
for (int i=0;i<stringword.length();i++)
{
if (stringword.find(','))
{
int value;
istringstream (stringword) >> value;
v.push_back(value);
stringword.erase(0, stringword.find(','));
}
}
istringstream (stringword) >> value interprets the data up to the comma as an integer, the first value, which is then stored.
stringword.find(',') gets you the 0-indexed position of the comma. A return value of 0 means that the character is the first character in the string, it does not tell you whether there is a comma in the string. In that case, the return value would be string::npos.
stringword.erase deletes that many characters from the start of the string. In this case, it deletes 10, making stringword ,200,3000,40000. This means that in the next iteration stringword.find(',') returns 0.
if (stringword.find(',')) does not behave as wished. if(0) casts the integer to a bool, where 0 is false and everything else is true. Therefore, it never enters the if-block again, as the next iterations will keep checking against this unchanged string.
And besides all that there's this:
for (int j=0;j<v.size();i++)
{
cout << v.at(i) << " " ;
}
it uses i. That was declared in a for loop, in a different scope.
The code you gave simply doesn't compile, even with the added main and includes. Heck, v isn't even defined in the second program.
It is however not enough, as the for condition stringword.length() is recalculated every loop. In this specific instance it works, because your integers get an extra digit each time, but let's say your input file is 1,2,3,4,:
The loop executes normally three times
The fourth time, stringword is 4, stringword.length() returns 2, but i is already valued 3, so i<stringword.length() is invalid, and the loop exits.
If you want to use the string's length as a condition, but edit the string during processing, store the value before editing. Even if you don't edit the string, this means less calls to length().
If you save length beforehand, in this new scenario that would be 8. However, after 4 loops string is already empty, and it executes the for loop some more times with no effect.
Instead, as we are editing the string to become empty, check for that.
All this together makes for radically different code altogether to make this work:
while (!stringword.empty())
{
int value;
istringstream (stringword) >> value;
v.push_back(value);
stringword.erase(0, stringword.find(',')+1);
}
for (int i = 0; i < v.size(); i++)
{
cout << v.at(i) << " " ;
}
A different way to solve this would have been to not try to find from the start, but from index i onwards, leaving a string of commas. But why stick to messy stuff if you can just do this.
And that's about it.