Thursday, 15 March 2012

C program to Calculate string length without using predefine function


/*Calculate string length without using predefine function in C language*/


#include<stdio.h>

void main()
 {
        char str[100]="I am the best",*ptr;
            ptr=str ;
         int count = 0,i;
           for(i=0;ptr[i]!='\0';i++)//can use also--while(*(ptr+count))
             {
                 count++;
              }
     
        printf("\nThe length of the string is %d.",count);

}//End of  main


:-Programming Error
carefully use ++ oprator and pointer.
loop should be run till null

:-Details of Programme
*ptr = its a pointer which is use for reference ;
count=0 count shuld initialize here with 0 because if its not then compiler assign garbage value to count and output will be diferent.


No comments:

Post a Comment

Compiler for C,C++ and Python {paste your programme to see the output}