'Get string lenght in C [closed]

I want to loop all the characters in a string to get the string lenght but it is not working. I prefer not using an already made function to do so.

#include <stdio.h>
#define N 100
int main()
{
    int i=0;
    char str[N];
    scanf("%s", &str);
    while(str[i]!="\0"){
        i=i+1;
    }
    printf("Lenght: %d", i);
    return 0;
}


Solution 1:[1]

For starters the call of scanf must look at least like

scanf("%s", str);

instead of

scanf("%s", &str);

In the condition of the while loop

while(str[i]!="\0"){

there is compared a characters with the string literal "\0" that is implicitly converted to a pointer to its first character. So there is compared a character with a pointer.

You need to compare two characters

while(str[i] != '\0' ){

or just

while(str[i]){

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1