What does this program do? loop through \0 terminated string - C++ Data Type

C++ examples for Data Type:char array

Description

What does this program do? loop through \0 terminated string

Demo Code

#include <iostream> 
using namespace std; 

int mystery2( const char * ); // prototype 

int main() //from www  . j  a v  a 2 s  .  co  m
{ 
   char string1[ 80 ]; 

   cout << "Enter a string: "; 
   cin >> string1; 
   cout << mystery2( string1 ) << endl; 
}

int mystery2( const char *s ) 
{ 
   int x; 

   for ( x = 0 ; *s != '\0'; ++s ) 
       ++x; 

   return x; 
}

Result


Related Tutorials