Circuit Negma

C++, C, VB.NET, PCB, Electronics, Circuit Design

alternative to method strlen()

Posted by Circuit Negma on February 6, 2006


#include <stdio.h>

main()
{
       int i;
       int count;
       char name[50];
      
       count = 0;
      
       printf(“please enter your first name: “);
       fflush(stdin);
       scanf(“%s”, name);
      
      for (i = 0; i <=50; i++)     // this is insted of using  strlen();
        {                            // note that in for statment is the condition of termination,
              if (name[i] == ”)   //   thus it can be replaced by the bottom loop.
              {
                count = i;
                break;
              }
        }
    
        for (i = 0; name[i] != ”; i++)  // nothing – do loop is used to write more simply and efficient code.
           {
            count = i + 1;
            }
       
       
       printf(“the length of >>> %s <<< is %d\n”, name, count);
      
        
          
       fflush(stdin);
       getchar();

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: