Click here to Skip to main content
16,020,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#include <stdio.h>

//function to calculate length of the string using recursion
int stringLength(char *str)
{
    static int length=0;
    if(*str!=NULL)
    {
        length++;
        stringLength(++str);
    }
    else
    {
        return length;
    }
}
int main()
{
    char str[100];
    int length=0;

    printf("Enter a string: ");
    gets(str);

    length=stringLength(str);

    printf("Total number of characters (string length) are: %d\n",length);

    return 0;
}


What I have tried:

run it but it work so i don't know where is worng.
Posted
Updated 30-Dec-17 2:09am
Comments
PIEBALDconsult 27-Dec-17 1:10am    
I don't see how that could work; are you sure it does?
Oh, wait, I see length is static -- so you're cheating, don't do that.

I can see multiple problems here.
1)The problem here is the static variable length. It will be initialized to zero only once in the program lifetime.

If you call the function one more time it will give you wrong result.

2) You are passing an uninitialized str array to the function. It can result in program hang or crash if it doesn't find the NULL character in subsequent memory locations.

The easy fix to this is to finding NULL in given limits.

Here the str is declared as
char str[100];

So we should check NULL in only 100 memory locations only.

You should pass the string input buffer length as a bound check.
 
Share this answer
 
Try this code to see what is funny
C++
int main()
{
    char str[100];
    int length=0;
 
    printf("Enter a string: ");
    gets(str);
    length=stringLength(str);
    printf("Total number of characters (string length) are: %d\n",length);
 
    printf("Enter a string: ");
    gets(str);
    length=stringLength(str);
    printf("Total number of characters (string length) are: %d\n",length);
 
    return 0;
}

-----
This code should not even compile.
Quote:
run it but it work so i don't know where is worng.

What do you mean by 'work'? if it don't crash, ok. if it give correct answer, it is more astonishing.
stringLength is missing half the logic. What are you doing with the result when you return from recursion?
Use the debugger to see exactly what your code is doing, pay attention to variables.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900