Pages

  • Home
  • About & Contact
  • Archive
  • C Programming Examples

28 February 2012

c program to find length of string without using strlen function [ using pointers]

Here i am showing program to find length of String in 3 different ways.

length of string
C program to find length of string using strlen().
.For information on Strings and string functions in c


#include <stdio.h>
#include <string.h>
#include<conio.h>
void main(void)
{
   char str1[25];
   int len;
   clrscr();
   printf("\nEnter String whose length is to be found:");
   gets(str1);
   len=strlen(str1);                                               //using Sting functions.
   printf("\nLength of the String %s is %d",str1,len);
}


C program to find length of string without using strlen().


#include<stdio.h>
void main(void)
{
   char str1[25];
   int len=0;
   printf("\nEnter String whose length is to be found:");
   gets(str1);

   while(str1[len]!='\0')
       len++;                                 //here the length of string is calculated. 

   printf("\nLengt of the String %s is %d",str1,len);
}

C program to find length of string without using strlen() using pointers.
Here we are creating our own Function to find the length of String.


#include<stdio.h>
int length(char* );
void main(void)
{
   char str1[50];
   int len;
   printf("\nEnter String whose length has to be found:");
   gets(str1);
   len=length(str1);                                           //1
   printf("\nlength of String %s is %d",str1,len);
}


int length(char *s1)
{
     int l=0;                                                    //2
     while(*s1!='\0')                                      //3
     {
           l++;                                                  //4
           s1++;                                               //5
     }
     
      return l;                                                 //6
}


Explanation:First program does not require any explanation as it just requires in-built strcat().
Second and Third program have much similar logic.So i am showing the logic behind the third program.

You can see //1  //2..
Consider Statement //1 Here we are calling our length function with 1 parameter i.e Base address of the String str1. We are collecting the base address in char pointer  s1.

Statement  //2 Just intializing the length variable to 0
Statement  //3 Here we are checking the condition for loop that the pointer has not reached to the end of the String.

Statement //4 Increamenting the value of l
Which will be increamented every time the loop terminates.

Statement //5 Traversing the pointer to the next character in the string.  


c program to compare two strings without using strcmp [ using pointers ]

10 comments:

  1. good explanation............

    ReplyDelete
  2. Anonymous24 July, 2012

    the explanation was good but you limited the string to a particular number .This pro gramme would help you to find string length of innumerable length






    # include
    # include
    main()
    {
    int j;
    char *i;
    clrscr();
    printf("please enter the string");
    scanf("%s",i);
    for(j=0;i[j]!='\0';j++);
    printf("the string length is %d",j);
    getch();
    }

    ReplyDelete
    Replies
    1. Thank you for your valuable improvement.

      Delete
    2. This comment has been removed by the author.

      Delete
  3. thats ok but i want to know how find a string length without using both the builtin functions and loop

    ReplyDelete
  4. hi
    this would work
    printf("%d",printf("omg"));

    ReplyDelete
    Replies
    1. Thanks for your valuable suggestion.
      I don't say you are not right, But for string length we have to print whole string again thats not good programming.

      Delete
    2. Thank u.

      Delete

Back To Top