20130504

How to find the length of a string without using library function strlen


/*How to find length of a string without using library function strstr.
How to use pointer/array to find length of string sentence. C++/CPP/C program/code to find length a string/word/sentence without using inbuilt function strlen.How to find the length of a string without using library function strlen*/
#include<stdio.h>
#include<string.h>

int strLen(char *p);

int main()
{
char arr1[20];
printf("Enter a String:\n");
scanf("%s",&arr1);

printf("You entered:%s\n",arr1);
int n;
n=strLen(arr1);
printf("Strlen of entered string is:%d\n",n);


return 0;
}
int length=0;

int strLen(char *p)
{
printf("Inside strLen\n");
char *temp;
temp=p;
while(*temp)
{
temp++;
}
return (temp-p);
}
/*
Enter a String:
India
You entered:India
Inside strLen
Strlen of entered string is:5
* */



1 comment:

  1. cpp program for finding length of a string:
    #include
    using namespace std;
    #include
    int main()
    {
    char str[]="howtocpp";
    int length
    length=strlen(str);
    cout<<"Length of given string is:"<<length<<endl;
    }
    //Length of given string is:8

    ReplyDelete