Input : <div5 id="abc" style="....">xyz</div5>
I want to assign this to a string variable but because of quote ,this cannot be directly assigned to a string variable .
How can I take this input .
So that I can assign to a string variable
You'll need to escape the quotes:
#include <iostream>
std::string x = "<div5 id=\"abc\" style=\"....\">xyz</div5>";
The same goes for Java:
String x = "<div5 id=\"abc\" style=\"....\">xyz</div5>;
To get it from the user in C++:
#include <iostream>
std::string x;
std::getline(std::cin, x);
And in Java:
String x = new Scanner(System.in).nextLine();
#include<iostream>
#include<strirng>
using namespace std;
int main()
{ string a,b;
getline(cin,a,'\"');//input end when there is a "
getline(cin,b,'\"');//because " is a wide space character,need to put\
before it
cout<<a<<" "<<b<<endl;
return 0;
}
Related
#include <iostream>
using namespace std;
int main(){
int a=4,b=8;
float r=5.55;
char c='comica';
cout<<"this is a test and value of surtd is"<<a<<".\n and value of sturd is"<<b;
cout<<"value of me is"<<'c';
return 0;
}
I did all things as shown in a lecture, but it is showing an error in the compiler.
Use the correct return 0 not return=0 and also char should hold one char not string
Code should be
#include <iostream>
using namespace std;
int main(){
int a=4,b=8;
float r=5.55;
char c='c';
cout<<"this is a test and value of surtd is"<<a<<".\n and value of sturd is"<<b;
cout<<"value of me is"<<c;
return 0;
}
char is used for storing single charecter. for storing a sequence of characters use std::string class.
string c = "comica";
//Note: Use double quotes for strings. single quotes are for charecter
Remove the quotes around variable c, Otherwise it'll print charecter c and not the value of string object c.
cout<<"value of me is"<<c;
as said in comments use return 0. return=0 is invalid.
There are multiple mistakes in your code:
Using a char variable initialized with a multi-byte character literal.
Initializing a float variable with a double literal (and then not even using the variable afterwards).
Passing a 'c' character literal to operator<< instead of your c variable.
Using a misplaced - in the return statement.
Try this instead:
#include <iostream>
#include <string>
using namespace std;
int main(){
int a = 4, b = 8;
//float r = 5.55f;
string s = "comica";
cout << "this is a test and value of surtd is " << a << ".\n and value of sturd is " << b;
cout << ".\n and value of me is " << s;
return 0;
}
I am trying to store a hex value in a string and latter retrieve it after some time, but while retrieving No value is coming size of the string is also coming 0. Sample code:
using namespace std;
int main() {
std::string s;
s.assign("\x00\x53"); // std::string s ="\x00\x53"
cout<<s.size();
}
output is coming 0
Try using \\ instead of \:
s.assign("\\x00\\x53");
Now you have:
#include <iostream>
using namespace std;
int main() {
std::string s;
s.assign("\\x00\\x53"); // std::string s ="\x00\x53"
cout << s.size() << endl;
cout << s << endl;
}
Output:
8
\x00\x53
From C++14 onwards, we have the option of using string literals, using that feature you can do this:
std::string s1 = "\x00\x53"s;
This will do what you expect and will return the correct value for size().
If you cannot use C++14 features, you need to use a string constructor that will allow you to specify the length of the string. You can do this:
std::string s1( "\x00\x53", 2);
You can see demo for both versions here.
I want to create a function that takes a string parameter, reverses it and returns the reversed string. There have been some answers, but none work fully.
Here is my code:
#include <iostream>
#include <string>
using namespace std;
string revStr(string word){
string reversed = "";
if(word.size() == 0)
{
return reversed;
}
for (int i = word.length()-1; i>=0; i--){
reversed = reversed+word[i];
}
return reversed;
}
int main(){
string strin;
cout << "enter string;" << endl;
cin>> strin;
cout << revStr(strin);
}
This works only for strings that do not contain a space. When I type in Hello World, it return olleH.
basic_string::operator>>:
2) Behaves as an FormattedInputFunction. After constructing and checking the sentry object, which may skip leading whitespace, first clears str with str.erase(), then reads characters from is and appends them to str as if by str.append(1, c), until one of the following conditions becomes true: [...]
std::isspace(c,is.getloc()) is true for the next character c in is (this whitespace character remains in the input stream).
The method you use by definition reads until a white-space, so you read only Hello into strin. You should use another method for reading like getline or stringstream.
You need to use std::getline to input strings with a space.
For reversing your std::string, consider using std::reverse from <algorithm>, although your algorithm is correct too.
Code:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string strin;
cout << "enter string;" << endl;
getline(cin,strin);
reverse(strin.begin() , strin.end() );
cout << strin;
}
See, cin halts the input at any occurrence of a space or a newline character. So, to input a string with spaces, you'd have to use cin.getline() and that can be done by using the following snippet:
string S;
cin.getline(1000,'\n');
This would take input till the newline character into string S and then we just have to reverse the string, and that can be done in two ways.
Method 1:
Using std::reverse from <algorithm> header file. This function works with all containers and takes iterators as parameters.
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
string S;
getline(cin,S);
reverse(S.begin(), S.end());
return 0;
}
Method 2:
You can create your function which swaps the characters at positions equidistant from end and start, and you get what you need in O(n) time-complexity.
#include <bits/stdc++.h>
using namespace std;
string myfunc(string S)
{
int l = 0;
int r = S.size()-1;
while(l<r)
{
swap(S[l],S[r]);
l++;
r--;
}
return S;
}
int main()
{
ios_base::sync_with_stdio(false);
string S;
getline(cin,S);
S = myfunc(S);
cout<<S;
return 0;
}
What I think is you could do fine with your revStr() but you need to get a whole line input, but using cin considers space as a delimiter, hence you get only Hello out of Hello World.
Replace cin >> strin with getline(cin,strin).
How can I extract a substring in c++ of all the characters before a * character. For example, if I have a string
ASDG::DS"G*0asd}}345sdgfsdfg
how would I extract the portion
ASDG::DS"G
You certainly don't need a regular expression for that. Just use std::string::find('*') and std::string::substr:
#include <string>
int main()
{
// raw strings require C++-11
std::string s1 = R"(ASDG::DS"G*0asd}}345sdgfsdfg)";
std::string s2 = s1.substr(0, s1.find('*'));
}
I think your text doesn't have multiple * because find return with first *
#include <iostream>
#include <string>
using namespace std;
#define SELECT_END_CHAR "*"
int main(){
string text = "ASDG::DS\"G*0asd}}345sdgfsdfg";
unsigned end_index = text.find(SELECT_END_CHAR);
string result = text.substr (0,end_index);
cout << result << endl;
system("pause");
return 0;
}
I want to get string input from the user. At the same time, I want to supply a default string so that if the user doesn't want to change it, they can just press enter. How can that be done in C++?
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* args[])
{
const string defaultText = "Default string";
string str;
string tmp;
getline(cin, tmp);
if (!tmp.empty()) //user typed something different than Enter
str = tmp;
else //otherwise use default value
str = defaultText;
cout << str << endl;
}
You should be able to do it with the version of getline() defined in . You can use it like this:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
getline(cin,str);
// Use str
}
Just use two strings: Default string and User_supplied string. Get the input from the user (for the user_supplied string) and do an strlen on this string to check if it has a length greater than zero. If so use the User_supplied string, else use the default string