size of a string?

hello i have a code and i have to know the size of the string for my loop.
i have searched the faq ive seen something with an inline function and a template but ican't compile it. i think that is because it has to be const but when its const i cant set a value to the string normal.
can someone tell me how to fix this or how i can reverse my string and how i can make all the r to l ?
here is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

//----------------------------------------------
//i have this part from the FAQ
template <typename T, size_t N>
inline
size_t SizeOfArray(  const T (&)[ N ] ) // i have tried to remove the const in here but that doesnt work to
{
  return N;
}
//-----------------------------------------------
int main () {

  int i;
  string normal,reversed,R_To_L;
  getline(cin,normal);
  for(i=SizeOfArray(normal);i>0;i--) {

	  reversed[i-1]=normal[SizeOfArray(normal)-i];

	  if(normal[i-1]='r')
	  { R_To_L[i-1]='l';}

	  else
	  {R_To_L[i-1]=normal[i-1];}}

  cout<<normal<<endl<<reversed<<endl<<RToL<<endl;
  
  system("pause");
  return 0;
}
Last edited on
This doesn't even come close...you have no strings in here.

Also, this problem would be easier if you looked at a string for what it is...a container, whose elements can be played with at will, as long as you know their index (hint, hint).

And last, but not least, you might want to look in on stringstreams. Although not strictly necessary, they are useful.
why i don't have strings?
i defined them under int i;
and what do you think the numbers in brackets are? aren't they the index ?
Last edited on
Don't change your code after the fact. I read your code before, they were NOT strings, they were char. And yes, i is the index, but you have to know how to use it in the context of containers to rearrange the letters inside of the container whose type is string.
template class std::string that you are using has a special member function size that returns size of a string.
For example,

size_t len = normal.size();
Topic archived. No new replies allowed.