Search code examples
c++string-length

Return value of function strlen()?


This is the definition of strlen i have goggled up.

strlen( ) function counts the number of characters in a given string and returns the integer value. It stops counting the character when null character is found.

Now according to me the strlen for "kshitij" should be = 7 i.e not including the null character because the function stops counting as and when it encounters the null character. Therefore if I want to print the word "kshitij" and its reverse as many times as the letters in the word, then the correct code should be.

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
    char a[1000];
    gets(a);
    int len=strlen(a);
    for(int i=0; i<= len ; i++)
    {
        for(int j=len ; j>=0; j--)
        {
            cout<<a[j];
        }
        cout<<" ";
    }
    getch();
     return 0;
 }

accordingly it provides the reasonably correct output.(Apart of spacing I don’t understand)

enter image description here

now I was curious to know what would it print if i did :

 #include<iostream.h>
 #include<stdio.h>
 #include<conio.h>
 #include<string.h>
 int main()
 {
    char a[1000];
    gets(a);
    int len=strlen(a);
    for(int i=0; i<= len ; i++)
    {
        for(int j=len -1 ; j>=0; j--)
        {
            cout<<a[j];
        }
        cout<<" ";
    }
    getch();
    return 0;
 }

which according to me should not produce the last letter which is "j" but what i see is that it produces the same output but 1 less spacing then before.

enter image description here

similarly i tried :

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
    char a[1000];
    gets(a);
    int len=strlen(a);
    for(int i=0; i<= len - 1  ; i++)
    {
        for(int j=len ; j>=0; j--)
        {
        cout<<a[j];
        }
        cout<<" ";
    }
    getch();
    return 0;
}

enter image description here

over here according to me the ouput should contain only " 7 - 1 = 6 " times the reverse string but the ouput almost the same.The -1 has no effect on the value of "len".

With all this on my plate , I feel that the strlen function must count the null character as well, wiz strlen (a)= 8 (including ‘\0’), but then I see only 7 outputs in the output window. This leaves me wondering if the strlen function counts the null character as well or not, and if it does then it must show it as a space in the output window. I am unable to apprehend the complete concept, any help is appreciated? I am new to programming please take it easy on me. Thanks :).


Solution

  • If you have a string with 7 characters, the array indexes of the printable characters go from 0 to 6, and strlen() will return 7. a[7] contains the null terminator character.

    So if you start your loop from j = len, the first character it prints is the null character, then it will print the printable characters in the remaining iterations. If you start your loop from len-1, it will print only the printable characters.

    The extra spacing you're seeing is that null character. On some operating systems, printing a null character has no visible effect, but on your system it apparently prints a space.