This code is giving error on 7th line?
why is *it=*rit not working?
string temp;
string::reverse_iterator rit;
rit = demo.rbegin();
string::iterator it;
it = temp.begin();
for (; rit != demo.rend(); rit++, it++)
{
*it = *rit;
}
To answer why your version doesn't work.
( I don't see a definition for demo - I assume its a non-empty string. )
temp is defined though, as an empty string.
Your loop checks "rit" but not "it" this could be OK if the 2 strings had the same length.
"it" is trying to iterate along an empty string, and that's why dereferencing it fails.
If temp was already a string with the correct length, then it could be iterated and edited successfully.
You could achieve this with
temp.resize(demo.length());
or just
temp = demo;
before the loop.
Try it like this:
#include <iostream>
#include <string>
#include <iterator>
int main()
{
std::string s = "Hello, world";
std::reverse_iterator<std::string::iterator> r = s.rbegin();
std::string rev(r, s.rend());
std::cout << rev << '\n';
}
Related
I have a c code to show a lowercase of one argument:
This code runs ok when called from commandline with more than 6 argument
however,if called without argument, the demo part does not work ,and programme got stuck:
Below is the code file:
Can anyone help me, Thanks!
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <ctype.h>
int showLowerCase(char *argv[]){
char* aStr = argv[6];
//To lowercase: the demo got stuck probably here
for (int i = 0; aStr[i]; i++) {
aStr[i] = tolower(aStr[i]);
}
printf("a lower case for 6th input string: %s\n",aStr);
}
int main(int argc, char *argv[])
{
if(argc < 7){
// when no srguments given, try a demo:
// change argc to 7
argc = 7;
// make a example of argv2 with 7 strings
char *argv2[7];
argv2[0] = "killWindowsVersatile.exe";
argv2[1] = "key";
argv2[2] = "ci";
argv2[3] = "once";
argv2[4] = "2";
argv2[5] = "1000";
argv2[6] = "SuperCol"; //this argument will be shown as lowercase
showLowerCase(argv2);
return 0;
}
showLowerCase(argv);
return 0;
}
It seems that argv can be modified to lowercase in site, while the argv2 I constructed cannot be modified.
Solved by strcpy:
int showLowerCase(char *argv[]){
printf("aStr = argv[6]\n");
char* aStr = argv[6];
printf("aStr = argv[6]...done");
//make hard copy
char newStr[strlen(aStr)+1];
strcpy(newStr,aStr);
//To lowercase
for (int i = 0; newStr[i]; i++) {
printf("i=0\n");
printf("%c\n",newStr[i]);
newStr[i] = tolower(aStr[i]);
}
printf("a lower case for 6th input string: %s\n",newStr);
}
I tried to write a code which gets an input from the user and concatenate with another string but it doesn't work well. The code is down below,
#include<iostream>
using namespace std;
int main() {
string s1="Hi ";
string s2;
cin>>s2;
s1=s1+s2
cout<<s1;
return 0;
}
Input:
this is how it works
Expected Output:
Hi this is how it works
But it didn't work as I expected. The output was:
Hi this
Can anybody help me?
'>>' reads space-delimited strings.
Now I found getline is used to read lines.
#include<iostream>
using namespace std;
int main() {
string s1="Hi ";
string s2;
getline(cin,s2);
s1=s1+s2;
cout<<s1;
return 0;
}
Now I get the desired output.
#include <iostream>
using namespace std;
int main()
{
string s1="hi ";
string s2;
cout << "Enter string s2: ";
getline (cin,s2);
s1 = s1 + s2;
cout << "concating both "<< s1;
return 0;
}
here use this! this should help!
Example
For example:
string MyString = "Normal\tString";
cout << MyString << endl;
produces the following: "Normal String"
Appending the raw string modifier to the string like so:
string MyString = R"(Normal\tString)";
cout << MyString << endl;
produces the following: "Normal\tString"
The Question
Is there a way to append the raw string modifier to a variable containing a string in order to print the raw form of the string contained within the variable?
string TestString = "Test\tString";
cout << R(TestString) << endl;
So you get: "Test\tString"
Is there a way to append the raw string modifier to a variable containing a string in order to print the raw form of the string contained within the variable?
No.
However, you can write a function that substitutes the characters that are defined by escape sequences by an appropriate string, i.e. replace the character '\t' by the string "\\t".
Sample program:
#include <iostream>
#include <string>
// Performs only one substitution of \t.
// Needs to be updated to do it for all occurrences of \t and
// all other escape sequences that can be found in raw strings.
std::string toRawString(std::string const& in)
{
std::string ret = in;
auto p = ret.find('\t');
if ( p != ret.npos )
{
ret.replace(p, 1, "\\t");
}
return ret;
}
int main()
{
std::string TestString = "Test\tString";
std::cout << toRawString(TestString) << std::endl;
}
Output:
Test\tString
This question is tagged as C++11, in which case rolling your own conversion function is probably the best call.
However, if you have a C++14 compiler, you can use the std::quoted stream manipulator:
#include <iostream>
#include <iomanip>
int main() {
string s = "Hello\tWorld!";
std::cout << std::quoted(s) << std::endl; // Prints "Hello\tWorld!"
}
Kind of. This works but it's not a pretty solution:
std::string strToInsert = " insert this "
std::string myRawString = R"(
raw string raw string raw string
raw string raw string raw string
)"; myRawString += strToInsert; myRawString += R"(raw string
raw string raw string raw string
)";
This program is giving wrong output,, basically i want to remove the character specified and replace it by 'g'...For e.g: All that glitters is not gold if the user entered o then the output should be All that glitters is ngt ggld but the program is deleting all the characters from n onwards
#include <iostream>
using namespace std;
int main()
{
string input(" ALL GLItters are not gold");
char a;
cin>>a;
for(int i=0;i<input.size();i++)
{
if(input.at(i)==a)
{
input.erase(i,i+1);
input.insert(i,"g");
}
}
cout<<"\n";
cout<<input;
}
string& erase (size_t pos = 0, size_t len = npos);
The second parameter ( len ) is the Number of characters to erase.
You have to put 1 not i+1 :
input.erase(i,1);
http://www.cplusplus.com/reference/string/string/erase/
Why not replace it directly? Replace your for loop with this:
for (char& c : input)
{
if (c == a)
c = 'g';
}
Live example here.
I've come across this
#define DsHook(a,b,c) if (!c##_) { INT_PTR* p=b+*(INT_PTR**)a; VirtualProtect(&c##_,4,PAGE_EXECUTE_READWRITE,&no); *(INT_PTR*)&c##_=*p; VirtualProtect(p,4,PAGE_EXECUTE_READWRITE,&no); *p=(INT_PTR)c; }
and everything is clear except the "c##_" word, what does that mean?
It means to "glue" together, so c and _ get "glued together" to form c_. This glueing happens after argument replacement in the macro. See my example:
#define glue(a,b) a##_##b
const char *hello_world = "Hello, World!";
int main(int arg, char *argv[]) {
printf("%s\n", glue(hello,world)); // prints Hello, World!
return 0;
}
It is called a token-pasting operator. Example:
// preprocessor_token_pasting.cpp
#include <stdio.h>
#define paster( n ) printf( "token" #n " = %d", token##n )
int token9 = 9;
int main()
{
paster(9);
}
Output
token9 = 9
That's concatenation that appends an underscore to the name passed as c. So when you use
DsHook(a,b,Something)
that part turns into
if (!Something_)
After the preprocessor, your macro will be expanded as:
if (!c_) { INT_PTR* p=b+*(INT_PTR**)a; VirtualProtect(&c_,4,PAGE_EXECUTE_READWRITE,&no); *(INT_PTR*)&c_=*p; VirtualProtect(p,4,PAGE_EXECUTE_READWRITE,&no); *p=(INT_PTR)c; }
The ## directive concatenates the value of c which you pass as a macro parameter to _
Simple one:
#define Check(a) if(c##x == 0) { }
At call site:
int varx; // Note the x
Check(var);
Would expand as:
if(varx == 0) { }
It is called Token Concatenation and it is used to concatenate tokens during the preprocessing
For example the following code will print out the values of the values of c, c_, c_spam:
#include<stdio.h>
#define DsHook(a,b,c) if (!c##_) \
{printf("c=%d c_ = %d and c_spam = %d\n",\
c, c##_,c##_spam);}
int main(){
int a,b,c=3;
int c_ = 0, c_spam = 4;
DsHook(a,b,c);
return 0;
}
Output:
c=3 c_ = 0 and c_spam = 4