1

I'm new in c++ and have come across a problem which I don't know what to make of. I was doing some examples in a book when I came across the following piece of code.

for(int i=0;string[i];i++){
    cout<<string[i];
}

Here instead of a boolean for the second argument the person has given the array index.Based on that method I ran the following piece of code but it didn't work.

char string[50] ="This is a test!";

for(int i=sizeof(string)-1;string[i];i--){
    cout<<string[i];
}

cout<<"\n";

I debugged it and the 'string[i]' has the value of 0. So my questions are,

  1. Why use an array index instead of a boolean (is it OK?).
  2. Why didn't my second piece of code work.

Thanks in advance.

1
  • 1
    String literals (and strings in general) end in a 0. Commented Oct 14, 2013 at 4:00

3 Answers 3

6

The boolean test string[i] is actually just checking if a null-character ('\0') is encountered. All strings in C end in a null-character, and so this works for forward-iteration through a string. But not if you are going in reverse. You should check instead that i is greater than or equal to zero.

Also, sizeof(string) returns the size of the datatype (50 bytes, or possibly more), not the length of the string it contains. To get the string length, use strlen:

for( int i = strlen(string) - 1; i >= 0; i-- ) {
    cout << string[i];
}

I would advise against calling your variables string, as this is a datatype in the standard library, and is confusing to C++ programmers if you use it as a variable name. Especially as people quite commonly import the entire std namespace and refer to the string class as just string.

This is like calling all your numeric values "integer". Doing so doesn't actually tell the programmer what it's used for, only what type it is. Please use more descriptive variable names =)

Sign up to request clarification or add additional context in comments.

5 Comments

small thing: string array not string class
I agree with the variable names, but since it was a test I didn't pay any attention to names. Anyway when I use strlen the array goes on for -values, any idea as to why?
I don't quite understand what you said there, @Madz - you mean that i takes on negative values? Perhaps edit your question and show some output that demonstrates what you're seeing, along with the code to reproduce.
@cramer I did make a mistake originally when I didn't notice that the OP wasn't using classes. The only time I refer to the string class now is when I'm actually talking about the string class.
@paddy my bad, I didn't notice the i >= 0. What I did was use 'for(int i=strlen(string)-1;string[i];i--)' and it printed a lot of gibberish at the end as i kept going on for negative integer values.
1

In the above for loop, a char has been used instead of bool as the second argument. In this case, non-zero values are considered as "true" and zero value is considered as "false".

In the second code, for loop does not execute because string[sizeof(string)-1] is zero and it is evaluated as "false".

If you use in the following way, your code will work

for(int i=0;string[i];i++){

Comments

1

Since this is C++, not C, you might be better off using the builtin string class rather than using character arrays.

Here is one way to print out the reverse of your string, character by character:

 #include <iterator>
 ...
 typedef std::reverse_iterator<std::string::iterator> rev_iter;

 string str("This is a freaking test!");
 for(rev_iter it (str.end()); it != rev_iter(str.begin()); ++it)
     std::cout <<  *it;
 std::cout << endl;

And another (but this modifies the string):

#include <algorithm>
...
string str("This is a freaking test!");
reverse(str.begin(), str.end());
cout << str << endl;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.