I'm writing a program that requires a string to be inputted, then broken up into individual letters. Essentially, I need help finding a way to turn "string" into ["s","t","r","i","n","g"]. The strings are also stored using the string data type instead of just an array of chars by default. I would like to keep it that way and avoid char but will use it if necessary.
7 Answers
string a = "hello";
cout << a[1];
I hope that explains it
3 Comments
Luca Matteis
i don't feel like it as i'm not here to give tutorials, just answers.
AshleysBrain
For the record, you can also just do: cout << "hello"[1];
Yakov Galka
@AshleysBrain: Or cout << 1["hello"];
A string is just a sequence of the underlying character (i.e. char for std::string and wchar_t for std::wstring).
Because of that, you easily get each letter:
for (std::string::size_type l = 0; l < str.length(); ++l)
{
std::string::value_type c = str[l];
}
1 Comment
Andreas Bonini
You're::gonna::scare::him::with::all::that::stuff. imo using
int or, even better, size_t will work on every existing platform and it's way more clear.Try using the c_str() method of std::string:
#include <string>
using namespace std;
int main(void)
{
string text = "hello";
size_t length = text.length() + sizeof('\0');
char * letters = new char[length];
strcpy(letters, length.c_str());
for (unsigned int i = 0; i < length; ++i)
{
cout << '[' << i << "] == '" << letters[i] << "'\n";
}
return EXIT_SUCCESS;
}
6 Comments
George
Adding sizeof('\0') to length isnt needed because it may be more than 1 and then the allocated array will have a higher size than needed. Adding 1 instead of sizeof('\0') is better.
paxdiablo
George, sizeof(char) is always 1.
Ryan
Not true! sizeof(char) is not always 1. I worked with a DSP chip where everything was at least 32-bit, char, short, int -- sizeof(char) == sizeof(short) == sizeof(int). The C standard just says that sizeof(char) <= sizeof(short) <= sizeof(int) but does not require sizeof(char) == 1. One of the side effects was that casting to char and BYTE to limit values to 8-bits had no effect since the data type was natively 32-bit. Granted this was an unusual architecture, but the C standard definitely does not say sizeof(char) == 1.
paxdiablo
No, @Ryan, I'm sorry but you're wrong. Section 5.3.3 "Sizeof" states: "sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1". A char is a byte but neither is necessarily an octet (8 bits). This is the same as with C. This is from the n3000 draft of C++0x but it's been that way for ages.
Ryan
Ah, I stand corrected. My real point was do not assume 1 == 8 bits which you clearly state. The interesting thing about the DSP chip was 1 == sizeof(char) == sizeof(short) == sizeof(int) which surprised me when I found that out where 1 == 32 bits.
|
string input ="some string for example my cat is handsome";
vector<string> split_char_to_vector(string input) {
vector<string> output;
for(size_t i=0;i<=input.length();i++) {
output.push_back(input[i]));
}
return output;
}
if you want to convert split strings into character the first traverse the string and write for each characters of string to the i'th position of char array ie
char array[1000];
std::string input="i dont think love never ends";
for(size_t i=0;i<=input.length();/*or string::npos;*/i++)
{
if (input[i] != '\0')
{
array[i] = input[i];
}
else
{
break;
}
}
for (size_t i = 0; i < 100; i++)
{
std::cout << array[i] << std::endl;
}
2 Comments
Jaydeep
Add the details also to make answer understandable.
Mark Ormesher
Welcome to Stack Overflow! While this code may answer the question, it is better to include some context, explaining how it works and when to use it. Code-only answers tend to be less useful in the long run. See How do I write a good answer? for some more info.
if you want to convert split strings into character the first traverse the string and write for each characters of string to the i'th position of char array ie
char array[1000];
std::string input="i dont think love never ends";
for(size_t i=0;i<=input.length();/*or string::npos;*/i++)
{
if (input[i] != '\0')
{
array[i] = input[i];
}
else
{
break;
}
}
//to avoid noise after position input.length();
for (size_t i = input.length(); i <= 1000; i++)
{
array[i] = '\0';
}
//ie return array; or print
for (size_t i = 0; i < 100; i++)
{
std::cout << array[i] << std::endl;
}
string?