Reputation: 107
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
string reverseString(char* str, int sizeArray)
{
char* first = &str[0];
char* last = &str[sizeArray - 1];
while(first < last)
{
char temp = *first;
*first = *last;
*last = temp;
first++;
last--;
}
return str;
}
int main()
{
int size_of_array;
cout << "Max size of input string: ";
cin >> size_of_array;
char* someArray = new char[size_of_array];
cout << "Enter in string: ";
cin >> someArray;
while(strlen(someArray)!=size_of_array);
{
cout << "Length of string doesn't equal size of array" << endl;
cout << "Enter in string: ";
cin >> someArray;
}
cout << reverseString(someArray, size_of_array);
}
When I cout strlen(someArray) and size_of_array they both are the same number, but when I run it I get the error in the while loop saying that they don't equal each other.
Example: I'll input 5 for size_of_array, then enter in the string Kevin. It'll give me the error.
Upvotes: 0
Views: 106
Reputation: 36896
You have an extra semicolon after your while statement which ends the condition. The following code block is then run unconditionally.
Upvotes: 1
Reputation: 27577
The actual char
array uses a \0
null character to placehold the end of the char *
string, strlen()
doesn't include this null character in it's length count.
Upvotes: 0