String Length Manual

String Length Manual

Easy C Strings 19 views
Explanation Complexity

Problem Statement

Calculate the length of the string without calling stop(). Count up to the null character in a loop. Understanding the logic is important.

Input Format

A string entered by the user.

Output Format

An integer representing the length of the string.

Example

"hello"
5

Constraints

• String ends with null character ''

• Do not use strlen()

Concept Explanation

In C, a string ends when the null character '' is found.
String length is the number of characters before ''.

Step-by-Step Explanation

1.Take the input string.

2.Initialize a counter length = 0.

3.Start from the first character of the string.

4.While the current character is not '':

5.Increment length by 1.

6.Move to the next character.

7.When '' is reached, stop the loop.

8.length now contains the total number of characters in the string.

Concept Explanation

In C, a string ends when the null character '' is found.
String length is the number of characters before ''.

Step-by-Step Explanation

1.Take the input string.

2.Initialize a counter length = 0.

3.Start from the first character of the string.

4.While the current character is not '':

5.Increment length by 1.

6.Move to the next character.

7.When '' is reached, stop the loop.

8.length now contains the total number of characters in the string.

Input / Output Format

Input Format
A string entered by the user.
Output Format
An integer representing the length of the string.
Constraints
• String ends with null character ''

• Do not use strlen()

Examples

Input:
"hello"
Output:
5

Example Solution (Public)

C
//loop logic breakdown 
while (str[length] != '\0') {
    length++;
}

Official Solution Code

//loop logic breakdown 
while (str[length] != '\0') {
    length++;
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.