Monday, 4 March 2013

Write a function of your own to find the length of a string (Without using the built in functions of C++).


Function in C++ to find the string length:

/*function to find the string length*/
int mystrlen(char str[]) /* int type function, it will accept an array of characters and return total number of characters */

{
          int i;
          for(i=0; str[i] !=’\0’; i++)
          {
           }
return i;
}/*function ends here*/

/*main() functions to implement the above function*/
void main()
{
          char str[100];
          int length;
          cout<<”Please enter a string : “<<endl;
          cin>>str;
          /*print the length of string using mystrlen function*/
          length= mystrlen(str);
          cout<<”Length of string” << str<<” is : “ <<length;
}