Concrete Geist
01-30-2002, 08:32 PM
Ok, this program must reverse a string and output it. The problem is not reversing the string, but rather outputting it. I must use string class, and when I try to output the string with cout, nothing happens. here is the code :
#include <iostream.h>
#include <string>
inline int strlen( const string& );
int main(void)
{
string name, reversename;
int index;
cin >> name;
for( index=0; index < strlen(name); index++ )
{
reversename[index] = name[strlen(name)-index-1];
cout << reversename[index];
} cout << endl;
return 0;
}
inline int strlen( const string& s )
{
int index;
for( index = 0; s[index] != '\0'; index++ )
; /* Nothing */
return index;
}
This program uses a for loop to output the string and that works well, but I can't use a loop. :(
#include <iostream.h>
#include <string>
inline int strlen( const string& );
int main(void)
{
string name, reversename;
int index;
cin >> name;
for( index=0; index < strlen(name); index++ )
{
reversename[index] = name[strlen(name)-index-1];
cout << reversename[index];
} cout << endl;
return 0;
}
inline int strlen( const string& s )
{
int index;
for( index = 0; s[index] != '\0'; index++ )
; /* Nothing */
return index;
}
This program uses a for loop to output the string and that works well, but I can't use a loop. :(