Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Range-For loop over a string adding a null or empty char at the end

Tags:

c++

Here is a loop that goes through each character in "(Level:". It adds something to the end which is messing up the rest of my code.

#include <iostream>

int main() {
    std::cout << "Output:" << std::endl;
    for (char letter : "(Level:") {
        std::cout << "'" << letter << "'" << std::endl;
    }
    return 0;
}

Output:

'('
'L'
'e'
'v'
'e'
'l'
':'
''

I'm new to C++ and I don't understand what's happening.

like image 447
Moncheeta Avatar asked Dec 20 '21 03:12

Moncheeta


People also ask

Why null character is put at the end of the string?

Justify your answer with example. A null character is a character with all its bits set to zero. Therefore, it has a numeric value of zero and can be used to represent the end of a string of characters, such as a word or phrase.

What happens if a string does not have a null at the end?

Nothing stops you from creating an array of characters and not ending it with a null-terminator, but using it as a null-terminated byte string will lead to undefined behavior.

Can Char be used in for loop?

In the first approach, you increment a char from a to z with each iteration of the for loop and print it out each time. In the second approach, you increment some offset from 0 to 25 and print out 'a' + offset . You mix these two approaches up in the first line.

How do you make a loop in a string?

For loops with strings usually start at 0 and use the string's length() for the ending condition to step through the string character by character. String s = "example"; // loop through the string from 0 to length for(int i=0; i < s. length(); i++) { String ithLetter = s.

How do you loop through a range in Python?

Python Looping Through a Range Python Glossary. The range() Function To loop through a set of code a specified number of times, we can use the range() function, The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number.

How to avoid null in a for loop?

Another way to effectively guard against a null in a for loop is to wrap your collection with Google Guava's Optional<T> as this, one hopes, makes the possibility of an effectively empty collection clear since the client would be expected to check if the collection is present with Optional.isPresent (). Show activity on this post.

How to loop through a set of code a specified number?

The range () Function. To loop through a set of code a specified number of times, we can use the range () function, The range () function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number.

What is range based for loop in C++?

Range-based for loop in C++. Range-based for loop in C++ is added since C++ 11. It executes a for loop over a range. Used as a more readable equivalent to the traditional for loop operating over a range of values, such as all elements in a container.


1 Answers

"(Level:" has type const char[8], and is equivalent to { '(', 'L', 'e', 'v', 'e', 'l', ':', '\0' }.

You can easily see this is the case by casting the letter to int before printing. Demo

This happens, because string literals (anything "...") are C-strings, which are zero terminated. If you want strings to have a size instead of a zero terminator, you can use a string_view literal:

#include <iostream>
#include <string_view>

using namespace std::literals::string_view_literals;

int main() {
    std::cout << "Output:" << std::endl;
    for (char letter : "(Level:"sv) {
        std::cout << "'" << letter << "' (" << static_cast<int>(letter) << ")\n";
    }
    return 0;
}

See the result

like image 114
gha.st Avatar answered Oct 23 '22 05:10

gha.st