Thursday, June 7, 2012
strlen function in C
strlen string function is the one of the most common function used regularly to find the length of the string in C programming language. It returns the length of the string excluding (not including) null character or '0円'. String library provides the inbuilt function for finding the length of the string and syntax is given below.
len is 14
strlen function definition: Below are the function for finding string length. I have given the code for finding strign length using both array and pointers.
strlen function with arrays:
Explaination: We will get the length of the string by counting the no. characters in the string. For this we need to traverse the string starting from zeroth character. In first method for arrays, we will traverse the string using index until reaches the null or terminal charater. Where as in second method for pointers ,we need to increase the pointer until pointer reaches the null or terminal characater.
int strlen(const char *str);Below is the sample code to find the length of string using strlen string function and later we will see our own function to find the string length.
main() { char str[]="this is string"; int len = strlen(str); printf("len is %d\n",len); }Output:
len is 14
strlen function definition: Below are the function for finding string length. I have given the code for finding strign length using both array and pointers.
strlen function with arrays:
int strlen_arry(char *str) { int len=0; while(str[len]!='0円') len++; return len; }strlen function with pointers:
int strlen_ptr(char *str) { int len=0; while(str!='0円') { str++; len++; } return len; }
Explaination: We will get the length of the string by counting the no. characters in the string. For this we need to traverse the string starting from zeroth character. In first method for arrays, we will traverse the string using index until reaches the null or terminal charater. Where as in second method for pointers ,we need to increase the pointer until pointer reaches the null or terminal characater.
Subscribe to:
Post Comments (Atom)
Popular Posts
-
A universally unique identifier ( UUID ) is an identifier standard used in software construction, standardized by the Open...
-
Recently I started working on Japser Studio professional for my new project Cloud to generate the reports. I was very new to all cloud ...
-
Below is C program for AVL Tree implementation. #include<stdio.h> #include<malloc.h> typedef struct bst { int info; int hei...
-
strcmp is another string library function which is used to compare two strings and returns zero if both strings are same , returns +ve valu...
-
One of the complex operation on binary search tree is deleting a node. Insertion is easy by calling recursive insertion. But deletion wont...
-
We have recently faced one tricky issue in AWS cloud while loading S3 file into Redshift using python. It took almost whole day to inde...
-
Object slicing: when a derived class object is assigned to a base class object. only base class data will be copied from derived class and...
-
We have faced lot of weird issues while loading S3 bucke t files into redshift. I will try to explain all issues what we faced. Before go...
-
Below code is to find the cube root of a given integer number with out using pow math library function. Its very simple and brute force...
-
Recently we faced one issue in reading messages from SQS in AWS cloud where we are processing same message multiple times. This issue we...
No comments:
Post a Comment