C Programming - Strings - Discussion

Discussion Forum : Strings - General Questions (Q.No. 7)
7.
Which of the following function is correct that finds the length of a string?
int xstrlen(char *s)
{
    int length=0;
    while(*s!='\0')
    {    length++; s++; }
    return (length);
}
int xstrlen(char s)
{
    int length=0;
    while(*s!='\0')
        length++; s++;
    return (length);
}
int xstrlen(char *s)
{
    int length=0;
    while(*s!='\0')
        length++;
    return (length);
}
int xstrlen(char *s)
{
    int length=0;
    while(*s!='\0')
        s++;
    return (length);
}
Answer: Option
Explanation:

Option A is the correct function to find the length of given string.

Example:

#include<stdio.h>

int xstrlen(char *s)
{
    int length=0;
    while(*s!='\0')
    { length++; s++; }
    return (length);
}

int main()
{
    char d[] = "IndiaBIX";
    printf("Length = %d\n", xstrlen(d));
    return 0;
}

Output: Length = 8

Discussion:
17 comments Page 2 of 2.

Harsh said:   3 years ago
@Ahmed;

Strlen gives output of string length excluding null character.

Ajay said:   10 years ago
I have problem in pointer. I didn't understand. Can you help me?

L.priyanka said:   9 years ago
I need more detail explanation about this problem.

Marcos said:   8 years ago
What exactly xstrlen() do than strlen()? Explain.

Ali said:   9 years ago
Please explain this program in detail.

Abay said:   6 years ago
what do strleni(); do? Please explain.

Prince varnwal said:   1 decade ago
What will do *s here?


Post your comments here:

Your comments will be displayed after verification.