A C program with a function that will return the length of a character string without using inbuilt function

By | October 10, 2013

Let’s identify variables needed for this program.

In this program, we need several variables to store string and count the number of characters in the given string.

First variable will be the one which will save the string entered by the user and it will be S[50]. In C language we do not have string as variable so we have to use character array for the same. Next is a pointer which will hold the address of S[50] and that is used to pass the character pointer but S itself is a pointer so no need to take any pointer. Next we will need a LEN variable to store Length of a characters in a string returned by the function so in all Two variables.
The identified variables are S[50],LEN.

For writing a function and using it in the C program, we should declare the function in the MAIN function. Declaration has to done in the area before the code starts, same area where we declare data variables.

int string_length(char*);
 char S[50];
 int LEN;

 

Now, Selection of data type will be int data type due to the values expected for counting are decimals and they will be holding smaller values so int data type is sufficient for LEN. Selection of data type will be char data type due to string is a character array and we don’t have any string data type so char data type is selected for S[50].

In this program, Give message to user to enter the required string and scan the enter character is the character array i.e. S[50]. Variable LEN is used for storing Length is assigned to the function call which will return integer value. Finally print the integer as Length of String entered by the user at the end of the program.

#include<stdio.h>
void main()
{
 int string_length(char*);
 char S[50];
 int LEN;
 clrscr();
 printf("\nENTER STRING TO FIND IT'S LENGTH : ");
 scanf("%s",&S);
 LEN=string_length(S);
 printf("\nLENGTH OF STRING IS :- %d",LEN);
 getch();
}

Let’s identify variables needed for function which is part of program outside main function.
Variables which are passed in the function may not be declared again which is  character pointer *STRS is passed to the function and saved in *STR variable of the function. Now the variable LEN which will hold the resultant value to be returned by the FUNCTION. Variable.  The identified variable is LEN.

Now, Selection of data type is int data type due to the values expected are decimals and they will be holding smaller values so int data type is sufficient.

int string_length(char *STR)
{
     int L=0;
     while(*STR)
     {
      L++;
      STR++;
     }
     return L;
}

In this program, we should not use inbuilt function so we are writing it in new function with new name. The base address of the string is passed to the function character pointer return true if any character is present in it or return false if any character is absent in it. Now pointer *STR which will be used for moving from start to end of the string given within the loop pointer is incremented and so is L which will count the character in the string and return length as result.

Note:- Remember whenever you are calling a function which is returning a value it should always be assigned to a variable of same data type or called inside the printf function as variable.

C program code :

  1. #include<stdio.h>
  2. void main()
  3. {
  4.  int string_length(char*);
  5.  char S[50];
  6.  int LEN;
  7.  clrscr();
  8.  printf("\nENTER STRING TO FIND IT'S LENGTH : ");
  9.  scanf("%s",&S);
  10.  LEN=string_length(S);
  11.  printf("\nLENGTH OF STRING IS :- %d",LEN);
  12.  getch();
  13. }
  14. int string_length(char *STR)
  15. {
  16.      int L=0;
  17.      while(*STR)
  18.      {
  19.       L++;
  20.       STR++;
  21.      }
  22.      return L;
  23. }
  24.      }

SCREEN SHOTS:-

C_program_String_Length_Function

C_program_String_Length_Function_Output

Leave a Reply