C Programming - Strings - Discussion

7. 

Which of the following function is correct that finds the length of a string?

[A].
int xstrlen(char *s)
{
    int length=0;
    while(*s!='\0')
    {    length++; s++; }
    return (length);
}
[B].
int xstrlen(char s)
{
    int length=0;
    while(*s!='\0')
        length++; s++;
    return (length);
}
[C].
int xstrlen(char *s)
{
    int length=0;
    while(*s!='\0')
        length++;
    return (length);
}
[D].
int xstrlen(char *s)
{
    int length=0;
    while(*s!='\0')
        s++;
    return (length);
}

Answer: Option A

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


Prince Varnwal said: (Sep 19, 2013)  
What will do *s here?

Prince Bansal said: (Apr 17, 2014)  
@Prince Varnwal:

*s is char pointer that holds character string.

When we want to print a string we use printf("%s",s). It will print whole string. It needs only base address.

s contains base address which is equal to &s[0] and printf will print characters until '\0' occurs.

Here, *s gives first character of input string where as s++ will increment base address by 1 byte.

When *s=='\0' encountered, it will terminate loop.

"Sorry for the poor English"

Shihab Hasan said: (Sep 22, 2014)  
Thanks a lot. You are great. Please write more and more so that we can learn C programming language. May God bless you.

Shihabhasan said: (Sep 22, 2014)  
sir I want to communicate with you.please response me.my skype id is:mehedi_cse40. please send me your skype id.

Ajay said: (Oct 9, 2015)  
I have problem in pointer. I didn't understand. Can you help me?

L.Priyanka said: (Jun 23, 2016)  
I need more detail explanation about this problem.

Uday said: (Sep 16, 2016)  
Here, *s prints the first character of a given string. Then we need to increment *s++ instead of scan. Can anyone give me reply for my doubt?

Harsh Patil said: (Dec 16, 2016)  
The string we have stored in the char D[] in main function will be pass the to the xstrlen(d) function and it will be parsed by *s and length will be counted by length++. So we need to have length ++ and s++.

Ali said: (Dec 21, 2016)  
Please explain this program in detail.

Akshay said: (Jan 22, 2017)  
What is the difference between strlen and xstrlen ?

If there is no any difference so, what is the significance of xstrlen in C programming?

Devi Achsah said: (Jul 25, 2017)  
I don't know exactly. But I can share I what know. In this program xstrlen is only the name of the function.

Marcos said: (Sep 21, 2017)  
What exactly xstrlen() do than strlen()? Explain.

Vivek Ladhe said: (Jun 27, 2019)  
@Akshay.

strlen is a std library function.
xstrlen is a user-defined function.

Abay said: (Aug 26, 2019)  
what do strleni(); do? Please explain.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.