This question already has answers here:
For every character in string
(10 answers)
Closed 8 years ago.
As the title says, how can I seperate a string into individual characters in c++? For example, if the string is "cat" how can I separate that into the characters c,a, and t?
Thanks
By using the operator[]. e.g.
std::string cat{"cat"};
if(cat[0] == 'c')
//stuff
If you're using std::string you can simply use .c_str( ) that will give you an array of the chars.
In c++11 you could also do:
for( auto c : a )
{
cout << c << '\n';
}
http://ideone.com/UAyxTo
If you want to store them in a vector:
string str("cat");
vector<char> chars(str.begin(), str.end());
for (char c : chars)
cout << c << endl;
Related
This question already has answers here:
std::string length() and size() member functions
(4 answers)
Closed 2 years ago.
We have some word, that we should identify the length of. How can I do that?
Example INPUT: "hello" - without quotes;
Example OUTPUT: 5
If input is contained in a std::string you can find the length as stated by Ravi.
If it's a C string you find the length with
int len = strlen(INPUT);
In C/C++ upper case is normally used for constants so it's better to name the string input, not INPUT.
string str;
cin>>str;
//use this
cout<<str.length();
//or use this
cout<<str.size();
both of them will work fine.
There is one function to find length in C++.
You can try by using:
input.length()
Also remember, you need to include:
#include <iostream>
using namespace std;
Refer this document :
https://www.w3schools.com/cpp/cpp_strings_length.asp
You can use input.length() or input.size().
Or you can use this simple loop.
for (i = 0; str[i]; i++)
;
cout << i << endl;
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;
This question already has answers here:
Why in the code "456"+1, output is "56" [duplicate]
(3 answers)
Closed 6 years ago.
I am doing some exercises in C++ when I came upon something not so clear for me:
cout << "String" + 1 << endl;
outputs : tring
I suggest it is something with pointer arithmetic, but does that mean that everytime I print something in quotes that is not part of previous defined array,I actually create a char array ?
A quoted string (formally a string literal) is an array of const char, regardless of whether your printing it or doing anything else with it.
Code:
cout << "String" + 1 << endl;
has the same effect as this:
const char *ptr = "String";
cout << ptr + 1 << endl;
so no you do not create a new array, you just change pointer and pass it to std::cout
This question already has answers here:
Case-insensitive string comparison in C++ [closed]
(30 answers)
Closed 5 years ago.
How do you accept case-insensitive and allow embedded blanks in a user input? So the user can enter “hong konG” and get a correct match to the input.
I only have the input[0] = toupper(input[0]); which only accepts if the case sensitive is at the beginning of the word.
while(true){
cout << "Enter a city by name: "<< " ";
std::getline (std::cin,input);
if (input == "quit")
{
break;
}
input[0] = toupper (input[0]);
//....how do I loop to find all letter's in the input string variable?
}
You can use a loop to convert the entire string to upper case one character at a time, but a better solution is to use C++ standard library's transform function for that:
std::string hk = "hong konG";
std::transform(hk.begin(), hk.end(), hk.begin(), ::toupper);
This would apply ::toupper to all characters of your string, resulting in a string that reads "HONG KONG".
Demo on ideone.
for (auto& c : str)
c = std::toupper(c)
You can convert the whole string to upper-case like this
for (size_t i = 0; i < input.size(); ++i)
input[i] = toupper (input[i]);
The other suggestion to use std::transform is also a perfectly good solution.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Alternative to itoa() for converting integer to string C++?
I want to convert an int variable to string to put it in the textbox.
I have tried this code:
int x = 123;
std::ostringstream osstream;
osstream << x;
std::string string_x = osstream.str();
But it doesn't work.
Try using stringstream instead
std::stringstream osstream;
osstream << x;
std::string string_x = osstream.str();
std::cout << string_x << std::endl;
Works on my computer.
I had another issue once where I had to append a whitespace to that, so try this too:
std::stringstream osstream;
osstream << x << " ";