Length of a input string

#include<stdio.h>
int main()
{

char country[200];
int length;
        while(1==scanf("%s",country))
        {
            length=string_length(country);
            printf("length:%d\n",length);
        }


    return 0;
}

int string_length(char str[])
{
    int i,length=0;
    for(i=0;str[i]!='\0';i++)
        {
            length++;
        }

    return length;
}

Another method:

#include<stdio.h>
int main()
{

    char country[200];
    int length;
    while(1==scanf("%s",country))
    {
        length=string_length(country);
        printf("length:%d\n",length);
    }
    return 0;
}

int string_length(char stro[])
{

    int i;
    for(i=0; stro[i]!='\0'; i++);
    return i;
}

With while loop:

#include<stdio.h>
int main()
{

    char country[200];
    int length;
    while(1==scanf("%s",country))
    {
        length=string_length(country);
        printf("length:%d\n",length);
    }
    return 0;
}

int string_length(char stro[])
{

    int i;
    i=0;

    while(stro[i]!='\0'){
    i++;
}
return i;
}

 

 

 

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published. Required fields are marked *