# include <iostream>
# include <string>
using namespace std;
int main ()
{
	string s1,s2;
	getline(cin,s1);
	for (int i=0;i<s1.length();i++)
	{
		if (s1[i]!='.' && s1[i]!=' ')
		{
			s2[i]=s2[i]+s1[i];
		}
		s2=s2+' ';
	}
}

//string subscript out of range error why??

s2[i]=s2[i]+s1[i];

What makes you think s2 is large enough to be indexed with s2?

How can you convert string to an string array?? Use pointers and increment the pointer to concatenate ur strings

string *str1,*str2;
for(;; )
if(str1++!=".")
str2++=str2++ + str1++;

Hope this is works

How can you convert string to an string array?? Use pointers and increment the pointer to concatenate ur strings

string *str1,*str2;
for(;; )
if(str1++!=".")
str2++=str2++ + str1++;

Hope this is works

There is WAAAY too much undefined behavior here. Not to mention you're de-referencing uninitialized pointers. Odds are, it won't work properly, if it even compiles.

>>How can you convert string to an string array??
Huh? What are you talking about? An std::string object has a built-in subscript operator, you don't have to convert anything. If you want to convert to a C-style string (a null-terminated array of char), you'll use the std::string::c_str() member function.

>>Use pointers and increment the pointer to concatenate ur strings
Possibly, but you're better off using either the class' built-in operator+= or append() function.

When you get right down to it, a std::string object is basically just a std::vector<char> with a few extra functions/operators. You can almost treat them the same. It is feasible (albeit, not really practical) to step through str1 char-by-char and perform a push_back() of each char in str1.

#include <iostream>
#include <string>

using std::cout;
using std::string;
using std::endl;

int main() {
  string str1("something"), str2("I'm gonna be ");

  for (unsigned i = 0; i < str1.length(); ++i) {
    str2.push_back(str1[i]);
  }
  std::cout << str2;

  return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.