C++ String manipulation using other than _str.

I am trying to write a program that will allow a user to enter a string of up to 80 characters and test the string to see if it is a palindrome. I do not have to worry about the user entering more than 80 characters. So far I have been able to enter a line into an array and copy the line to another array reversed. I had planned to compare the cells of the array to test for a match but I am not sure what is in the cell after the last character if the user entered string is less than the size of the array. I think there is a null at the end of the array placed there by cin.getline. I thought that if the user entered a string less than the array size that the enter key would put in a null or maybe a "\r". But my test seems to not be working. Currently, I have limited the array size to 10 in an effort to understand the process. I am trying to setup a loop to count the number of characters the user entered so I can use that value to limit what is copied and how it is compared in the arrays. Also, to exit the program the user must enter "END". I had planned on trying to use an if statement and an expression in the array subscript to test 3 consecutive cells for "END". I have extra outputs in the program to help me figure it out.

I wrote the program using the string functions and it worked well.

I need someone to give me some advice on where my mistakes are and on how I should proceed.


Last edited on
Use std::strlen() in header <cstring> to determine the size of a c-style string.

Note: std::strlen() is not really required for this; std::cin.gcount() would give the number of characters extracted, but we will just ignore this nicety for now.


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

int main()
{
    enum { MAX = 80 } ;
    char cstr[ MAX + 1 ] ; // one extra for the terminating null char
    std::cout << "Enter a line of text to test for a palindrome. " ;
    std::cin.getline( cstr, MAX+1 ) ;

    std::size_t n = std::strlen(cstr) ; // number of chars entered by the user
    // each char can be accessed by str[i] where i is in [ 0, 1, 2, 3 , ... , (n-1) ]

    // TODO: test if cstr contains a palidrome

    // > Also, to exit the program the user must enter "END".
    // use std::strcmp() for this http://en.cppreference.com/w/cpp/string/byte/strcmp
    if( std::strcmp( cstr, "END" ) == 0 ) { /* the user entered the string "END" */ }
}

Take it up from there.
Last edited on
Thanks JLB,

But, I cannot use any string methods. A friend pointed out a logic error and now I have it working.

Last edited on
As I have understood you should write similar functions as in cstring by yourself.
Fot example the function that returns length of a string will look the following way

1
2
3
4
5
6
7
8
size_t StringLen( const char *s )
{
   size_t len = 0;

   for ( ; *s; ++s ) ++len;

   return ( len );
}


As for your code it would be more correctly to include the following headers

1
2
3
4
5
#include <iostream>
#include <cstdlib>

using namespace std;
...


main is declared as int main()

instead of while ( true == 1 ) would be better to write while ( true ) because true is always converted to integer equal to 1.

Instead of setting string to blanc you could define your character string inside loop as local varibales and set them to zero. For example


1
2
3
4
5
6
7
8
9
10
11
12
13
14
while ( true )
{
   char Line [MaxLength + 1] = {};  
   char ReverseLine [MaxLength + 1] = {};

   cout << "Enter a line of text to test for a palindrome." << endl;
   cout << "\n";

   cin.getline ( Line, MaxLength + 1 );
   cin.clear ();

   size_t len = StringLen( Line );

   // and so on 



Topic archived. No new replies allowed.