push_back a char from a string to another string [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I'm trying to append some characters from one string to another, but I couldn't do it. I tried something like this:
std::string fooz = "fooz";
std::string foo;
int i = 0;
while(i< fooz.length()){
if(fooz[i] != 'z'){
foo.push_back(fooz[i]);
}
i++;
}
foo after the while its empty.

You're taking length from the target string, which is still empty and the while loop won't be executed at all.
Change
while(i< foo.length()){
to
while(i< fooz.length()){

The STL can help you in this kind of scenarios.
This one uses the remove algorithm which provide a range of element to erase.
#include <string>
#include <iostream>
#include <algorithm>
int main()
{
std::string str("aaazbbb");
std::cout << str << std::endl;
str.erase(std::remove(str.begin(), str.end(), 'z'), str.end());
std::cout << str << std::endl;
}

std::string fooz = "fooz";
std::string foo;
int i = 0;
int len=fooz.size();
while(i< len){
if(fooz[i] != 'z'){
foo.push_back(fooz[i]);
}
i++;
}
Don not call std::string.size() or length() in while loop.

Related

Replace a part of a string [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 months ago.
This post was edited and submitted for review 9 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
Annotation:
In my code I have edited the string at an adneren position, this I have not had in my example.
Since here the actual error cause/problem lay I have adapted the example, see example below.
The Answer should be:
std::string.resize increases the memory size of the string to the maximum expected size.
replace inserts the entire size into str0.
A solution is to execute resize only if str1.lengh > max_len.
Original question:
I would like to insert a string (str1) into another (str0), at any position (n).
If str1 is shorter than the rest of string str0 (n -> str0.end()), the rest of str0 should remain.
For this I wanted to use the std::string::replace() function.
#include <string>
#include <stdint.h>
#include <iostream>
int main()
{
std::string str0 = "ABCDEFGHIJ";
std::string str1 = "000";
uint16_t n = 3;
str0.replace(n, str1.length(), str1);
std::cout << "TEST:" << str0 << std::endl;
return 0;
}
In my example, I expect to output "TEST: ABC000GHIJ" via cout.
However, "TEST: ABC000" is output.
Where is the problem that the string will be cutted?
Correct code example with the error cause:
#include <string>
#include <stdint.h>
#include <iostream>
#define max_len 20
int main()
{
std::string str0 = "ABDCDEF";
std::string str1 = "";
uint16_t n = 3;
std::getline(std::cin, str1);
if(!str1.empty() && str1[str1.size()-1] == '\n') {
str1.erase(str1.size()-1);
}
str1.resize(max_len);
str0.replace(n, str1.length(), str1);
std::cout << "TEST:" << str0 << std::endl;
return 0;
}

Can't use convert upper to lower character [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 11 months ago.
Improve this question
If I execute this code
#include <iostream>
using namespace std;
int main()
{
string word;
getline(cin, word);
for (char ch: word) {
if (isupper(ch))
ch=tolower(ch);
}
cout<<word<<endl;
return 0;
}
The characters don't convert to lower cases...
What could have gone wrong?
Consider this piece of code:
char a = 'x';
char b = a;
b = 'y';
What is the value of a at the end? It is 'x' because a and b are distinct objects, and modifying b has no effect on the value of a.
Same applies to your code. The loop variable:
for (char ch: word)
^^^^^^^
is a distinct object from the element of the string. Modifying that object has no effect on the string. In order to modify the string, you need to use indirection. Use a reference to char instead of a char:
for (char& ch: word)
The issue with your code snippet is that you modify the copy of each character instead of the characters within the string.
In other words, in your loop, you need to take a reference of each character:
for (char& ch: word)
Here is a quick code snippet that does what you want to achieve:
#include <iostream>
#include <string>
#include <cassert>
static bool convert_str_to_lower(std::string* word) {
if (word == nullptr) return false;
for (char& ch : *word) {
if (std::isupper(ch)) ch=std::tolower(ch);
}
return true;
}
int main() {
std::string word = "FoOBaR";
assert(convert_str_to_lower(&word));
assert(word.compare("foobar") == 0);
std::cout << "converted str: " << word << "\n";
return 0;
}

Having an unexpected output when reversing a string in C++ [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I wrote a simple code to reverse a string but I get a strange output. For example, if I type "hello" I get at output " qlleh" and I sincerely don't know why.
Here is the code:
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
int a = 0;
char s[1024],r[1024];
cout<<"Enter a string:";
cin>>s;
for(int i = char_traits<char>::length(s); i >= 0; i--){
//if(isalpha(s[i]))
r[a++] += s[i];
}
cout<<r;
}
You want to use a '=' only. A '+=' will add onto and increment the actual character value. Therefore, 'a' + 2 = 'c'.

The output begins with space [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I made a function that will reverse the string, but the output of the reversed string always shifts towards the right by one character.
#include <iostream>
#include <string>
using namespace std;
void reverse(string string1)
{
cout << endl;
for (int i = string1.size(); i >= 0; i--)
{
cout << string1[i];
}
cout << endl;
}
int main()
{
string string1;
getline(cin, string1);
reverse(string1);
system("pause");
return 0;
}
Your first output is of a character that does not exist.
std::string's leaky abstraction means that your first iteration is printing '\0', which apparently looks like a space in your configuration.
Begin at string1.size() - 1.

why am I getting random results when incrementing an int [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have to write a function that goes through a string, finds any capital letters present and makes them lowercase. I decided to add a bit of code to show what letters were found and how many were found. Although on each compilation, the value of 'cnt' yields ridiculous results.
#include <iostream>
#include <vector>
using namespace std;
int upper(string s) {
int cnt;
vector<char> v{};
for (auto& i : s) {
if (isupper(i)) {
v.push_back(i);
i = tolower(i);
++cnt;
}
}
cout << "new string is '" << s << "'\n"
<< "number of capitals found is " << cnt << "\n"
<< "letters found were ";
for (auto l : v)
cout << l << " ";
return 0;
}
int main() {
string l = "This IS a TeSt";
upper(l);
}
I'm sure I must have done something wrong with the loop but whatever the problem is, I cannot find it.
The variable cnt is never initialized when used, change
int cnt;
to
int cnt = 0;
You failed to initialize the local variable cnt. Using an uninitialized value provokes undefined behavior, under which basically anything can happen.
Use int cnt=0; and please turn on all your compiler warnings.