Tuesday, 28 October 2014

String leangth using without library functions

Aim: String length using while or for loop.

Program:
Code-1:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main ()
{
  char str[80];
  int length=0,i=0;
clrscr ();
printf("Enter a string:");
gets(str);
  while( str[i] != '\0' )
    {
      length++;
       i++;
    }
printf("\nLength of a string is : %d",length);
}

Code-2:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main ()
{
  char str[80];
  int length=0;
clrscr ();
printf("Enter a string:");
gets(str);
  for( i=0; str[i] != '\0'; i++ )
    {
      length++;
    }
printf("\nLength of a string is : %d",length);
}

No comments:

Post a Comment