Delete characters form a string in C++ [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm beginning in C++ and I have a simple task. As title said, I want to delete first and last character from a string for x times (where x is the lenght of the string). For example, if the string is "example", the result will be:
example
xampl
amp
m
amp
xampl
example
So far, I'm thinking like this:
#include <iostream>
#include <string>
string sir = "Example";
int len, i;
len = sir.length();
for(i=1; i<=len; i++)
{
sir.erase(sir.begin(), sir.end());
cout<<sir;
}
Or something like that... Can someone help me ?

You want to delete both the first and last char.But in the example you also added them each step. It is not actually clear what you want. Whatever you want to delete or add the characters it is feasible to keep the string unchanged. So you should use substr. Check it out here.

The problem is that
you can not use std::string::erase with integral index such as int i,
you need to use std::string::iterator
but even if you use std::string::iterator, with the current logic you would be trying to increment an iterator after the erase has been called. (such iterator is invalid)
Possible solution: assign sir.begin() to your iterator after each erase.
Here's how it could look like:
std::string sir = "Example";
for(string::iterator i = sir.begin(); i != sir.end(); i = sir.begin())
{
sir.erase(i);
std::cout << sir << std::endl;
}
outputs:
xample
ample
mple
ple
le
e
Just note that after erasing characters from your std::string, these characters are lost. You can not "restore" them. For the other half, you'll have to come with more sophisticated approach, but I'll leave that to you :)

Related

Why ı cant print the string? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am new at the c++. I made basic random string program but I cant print the string to console. These are my codes :
you did not include <string> and also you take the string by copy, to edit it you'll have to pass it by reference using &
#include <string>
void randomString(std::string& line)
you don't need to assign a number to int and then assign it to char, char is an integer value in c++ so you can:
char character = rand()%122 + 97;
with this method your random numbers will not be very good, you can look into How to generate a random number in C++?
you access your string with [] operator and it has not yet been defined, to add a character to a string just use
line += character;
also if you want a random number length, there is no need for the boolean and i++ stuff:
int charNumb = rand() % 8 + 4;
while(charNumb--)
will do just fine and it looks much cleaner.

wrong printing 2D char array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I am doing my homework and I declared a 2D array but it's not printing what I want.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char NOTE[7][3] = { {'D','O'} , {'R','E'} , {'M','I'} , {'F','A'} , {'S','O','L'} , {'L','A'} , {'S','I'} };
for (int i = 0; i <7 ; i++)
cout <<setw(10)<< NOTE[i] << "\n";
return 0;
}
I want to get:
DO
RE
MI
FA
SOL
LA
SI
DO
but I got:
DO
RE
MI
FA
SOLLA
LA
SI
DO
NOTE[i] is an array of characters. << NOTE[i] inserts this array into a character stream. The array argument decays to a pointer to the first character.
The documentation says that behaviour is undefined unless the pointer operand points to a null terminated array. The array {'S','O','L'} is not null terminated. Therefore the behaviour of the program is undefined - although it could be argued that the behaviour of iterating across boundaries subarrays of a multidimensional array should be well defined (with the behaviour shown in your program), but strict interpretation of the standard is that doing so is UB.
So, to get the output you want, you either need to 1. null terminate each sub array (and therefore need a larger array), or to 2. print each character individually.

C++ Reading Characters from Text File to Dynamically Allocated Array [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am relatively new to C++, and I am attempting to read sequenceofchars from a text file into a char array that is dynamically allocated. My code is as follows:
while (file.get(c))
{
if (c =='\n')
continue;
char *temp = new char[i++];
arrayA = new char[i++];
arrayA[i] = c;
delete [] arrayA;
arrayA = temp;
}
And the text file format is as follows:
>NameOfChars
sequenceofchars
This is obviously horribly broken, but I've struggled to figure out the exact methodology one would use to go through this. I know about the Vector class, but I am unsure about how to go about using that if that is the preferred method for reallocating arrays on the heap. Any help would be greatly appreciated. Thank you.
I think you should definitely take a look at the vector class since it would make your code a lot cleaner. Here is a small (untested) code sample of how to use it:
#include <vector>
std::vector<char> my_vector;
while (file.get(c))
{
if (c =='\n')
continue;
my_vector.push_back(c);
}
For more information please check http://www.cplusplus.com/reference/vector/vector/push_back/
A raw array isn't dynamically allocated; hence using an STL container like vector would be better.
ifstream inf;
char c;
vector<char> charVec;
while (inf >> c)
{
charVec.push_back(c);
}

c++ 2D char array element initializing with const char [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am trying initialize element of array
array[m][n] == char("X");
after printing that element i'm getting the value of it equals д (Russian d); how deal with it, and I'm not even able to initialize that element without parsing const char to char.
You have to write simply as
array[m][n] = 'X';
where 'X' is a character literal.
Or if you like very much string literals then:)
array[m][n] = *"X";
or
array[m][n] = "X"[0];
EDIT: I am sorry. You have also to use the assignmnet operator (=) instead of the comparison operator (==)

code infinite loop cannot print only once [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
hi I have this simple code to print out book word when i is equal 2
int i;
for (i=0;i!=5;i++)
{
if(i=2)
{
cout << "book";
};
}
i dont know why this code always looping even though I limit i to 5.
if I print i before if condition its printing this
0book3book3book3book3book3book3book3book3book3book3boo....
im using online compiler to compile this.
anyone know how to solve this?
if(i=2)
should be
if(i==2)
Classic mistake
if(i=2) --> assignment operator
if(i==2) --> comparing. Very different.
if(i=2)
is equivalent to
i = 2;
if(i != 0)
you need
if(i == 2)
Yeah as Roger said what you have done is do an assignment within the if statement instead of a comparison.
if(i=2) //assignment
if(i==2)//comparison
personally when comparing a variable to a raw int etc I like to do the following:
if(2==i) //comparison no error
if(2=i) //attempt to assign to raw int :ERROR
Instruction
if(i=2)
mean: assign value 2 to i and check if i!=0.
You had to do:
if(i==2)
Check if i is equal to value 2.
Simply syntax.