Problem with strings and characters

Pages: 12
Jul 6, 2016 at 6:11am
Hi guys,

How can I convert a string into a sequence of characters. I know that I can use char str[]="hello world" for example, however in my case my string is a changing variable. In my problem I read the first column from a text file and then store in a vector of strings, then I want to convert these strings into a sequence of characters. I have attached part of my code and I dont know how to proceed. As you can see I store the first column of my text file into a vector of strings. Now I want to change that vector of strings into a sequence of characters. Any I ideas how to do it. A little bit more information. The strings stored in a vector does not have the same length.

Thanks,


#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <math.h>
#include <string>
#include <stdio.h>

using namespace std;

int main()
{
string id;
double x,y,z;

vector<string>cntnumber;
ifstream sengab;
sengab.open("/Users/ahmedsengab/Downloads/CNT_Location_200_200.txt");
if (!sengab){cout<<"unable to open file"<<endl;exit(1);}
int i=0;
while(!sengab.eof())
{
sengab>>id>>x>>y>>z;
cntnumber.push_back(id);
cout<<cntnumber[i]<<""<<endl;
i++;
}
}
/
Jul 6, 2016 at 7:31am
closed account (48T7M4Gy)
convert a string into a sequence of characters

A string IS a sequence of characters whether its a c-style string char mystring[] = "string";or a string object std::string someString; as defined in #include<string>

The variability of string length is much easier to handle and less error-prone using std::strings
Last edited on Jul 6, 2016 at 7:34am
Jul 6, 2016 at 8:19am
Sorry I dont get what you mean. Can you post an example of what you mean
Jul 6, 2016 at 8:31am
closed account (48T7M4Gy)
http://www.cplusplus.com/doc/tutorial/ntcs/
Jul 6, 2016 at 8:34am
> I want to convert these strings into a sequence of characters

Is it what you meant :
1
2
3
4
5
6
7
 // After reading file
string bigString;

for (std::vector<string>::iterator it = cntnumber.begin() ; it != cntnumber.end(); ++it)
  bigString += *it;

cout << bigString << endl;
Jul 6, 2016 at 8:40am
Or :
1
2
3
4
5
6
7
8
 // After reading file
string bigString;

for (std::vector<string>::iterator it = cntnumber.begin() ; it != cntnumber.end(); ++it)
for (string::iterator it2 =it->begin(); it2 != it->end(); ++it2)
    bigString += *it2 + ", ";

cout << bigString << endl;
Jul 6, 2016 at 8:46am
No this is is not what I mean. I want to break each string into character variables. For example, If my string is "text" I would like to store as x='t',y='e', z='x' and k='t'; What you wrote actually reprints the string that is stored in cntnumber vector. I want to break each string in that vector and store it in a character vector. For example vector<char> x for the first character, vector<char> y for the second character, vector<char> z for the third character,
Jul 6, 2016 at 8:57am
closed account (48T7M4Gy)
1
2
3
4
5
string start = "text";
char x = start[0];
char y = start[1];
char z = start[2];
char k = start[3];


OR, more to the point see and modify http://www.cplusplus.com/reference/vector/vector/at/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// vector::at
#include <iostream>
#include <vector>

int main ()
{
  std::vector<char> myvector (10);   // 10 zero-initialized ints

  // assign some values:
  for (unsigned i=0; i<myvector.size(); i++)
    myvector.at(i)='*';

  std::cout << "myvector contains:";
  for (unsigned i=0; i<myvector.size(); i++)
    std::cout << ' ' << myvector.at(i);
  std::cout << '\n';

  return 0;
}
myvector contains: * * * * * * * * * *
Last edited on Jul 6, 2016 at 8:58am
Jul 6, 2016 at 9:04am
If my string is "text" I would like to store as x='t',y='e', z='x' and k='t';

You can simply use the array notation to access each individual character, no need to assign different variables.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>

using namespace std;

int main ()
{
   string s = "text";
   
   cout << "The characters are:\n  ";
   for (unsigned i = 0; i<s.size(); ++i)
   {
       cout << s[i] << "\n  ";
   }
}
The characters are:
  t
  e
  x
  t


I want to break each string in that vector and store it in a character vector.

If you really wanted to do that, it isn't hard. Though I'd consider it probably not necessary.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main ()
{
   string s = "text";
   vector<char> vec(s.begin(), s.end());
   
   cout << "The characters are:\n  ";
   for (unsigned i = 0; i<vec.size(); ++i)
   {
       cout << vec[i] << "\n  ";
   }
}
The characters are:
  t
  e
  x
  t

Jul 6, 2016 at 9:11am
I want to break each string in that vector and store it in a character vector. For example vector<char> x for the first character, vector<char> y for the second character, vector<char> z for the third character,

Do you mean the char vector would contain the first letters of each of the strings, another vector would contain the second letters of each of the strings and so on? I'm not quite sure whether i understood.
Jul 6, 2016 at 9:13am
> For example vector<char> x for the first character, vector<char> y for the second character, vector<char> z for the third character

1
2
3
4
5
6
7
8
std::vector<string> xyzk(120, string());

for (std::vector<string>::iterator it = cntnumber.begin() ; it != cntnumber.end(); ++it) 
for(int i = 0; i < it-> length(); ++i) xyzk[i] += (char)((*it)[i]);

cout << "Sequences of characters : " << endl;
for(int i = 0; i < xyzk.size(); ++i)
    if(xyzk[i].length() > 0) cout << i << " : " << xyzk[i] << endl; 


Edit : Some logic errors fixed.
Last edited on Jul 6, 2016 at 9:33am
Jul 6, 2016 at 9:17am
Is it what you want? :)
Last edited on Jul 6, 2016 at 9:17am
Jul 6, 2016 at 9:32am
closed account (48T7M4Gy)
Poor bugger is flooded with alternatives.
Jul 6, 2016 at 9:38am
> Poor bugger is flooded with alternatives.
Do you mean mine? My solution is buggy?

Nevermind, I have already fixed it.
Jul 6, 2016 at 9:49am
closed account (48T7M4Gy)
No, I don't mean anybody's valuable contributions and I didn't read yours closely enough to say if its buggy or not. It will be interesting to see what happens with this large volume of info. Saturation perhaps. Who knows?
Jul 6, 2016 at 1:50pm
Thanks for the reply guys, but I think no one got what I meant. i Store my data in a string vector. I want to access the characters in the each string component of that vector. The problem with my string vector I can't use literals because my vector is large 2000 items. . I know how to access each string in my vector compoenent but I dont know how to convert it to an array of characters. If it is string literal i.e "" I can convert it easy but that is not the case. When I mentioned "text" it was an example. In my case my string vector has 2000 strings, they are not string literals. Lets say these are strings stored in my vector.

text
100009
kevin
nick.
etc.. till 2000 entries

Now how can you covert each string of these to characters so for examples
text -----would be 't','e','x','t'
10009 -----would be '1','0','0','0','9'
etc
Jul 6, 2016 at 2:04pm
Why do you need to convert a string to an array of characters?

By the way a string is basically an array of char. You can access each character of a string using array notation.

1
2
3
4
std:: string readFromFile = "text"; // Note you would replace "text" with the value read from your file.

std::cout << readFromFile[2] << std::endl;  // Should print 'x'.


Jul 6, 2016 at 2:20pm
I need these characters to operate on them.
this is does not work for std:: string readfromfile=cntnumber;
it is not possible.
it has to be equal to a string literal i.e std:: string readfromfile="cntnumber";
Jul 6, 2016 at 2:25pm
> this is does not work for std:: string readfromfile=cntnumber;
it is not possible.

Then how about :
std:: string readfromfile=cntnumber[0];
Jul 6, 2016 at 2:26pm
perfect this actually works thanks a lot
Pages: 12