ccplusplus.com
Learn C, C++ Concepts
Friday, October 7, 2011
calculate length of string in c
/************************************************************* * File : calculate-length-of-string.c * Author : Saurabh Gupta * Desc : find length of string in c * find length of string without using strlen * Source : http://saurabhgupta0527.blogspot.com/p/c.html * Created : AM 09:00 10 October 2011 * ToDo : check the string validation also ************************************************************/ #include <stdio.h> int getLengthOfString (const char*); int main ( ){ char *cpString; printf ("Enter the string you want to calculate length : "); scanf ("%s", cpString); int nStringLen = 0; nStringLen = getLengthOfString(cpString); printf( "Length of the Given String is : %d \n", nStringLen); return 0; } int getLengthOfString(const char *cpStrTemp) { const char *anchor = cpStrTemp; while(*cpStrTemp) { cpStrTemp++; } return (cpStrTemp - anchor); } /* * OUTPUT * [sgupta@rhel54x64 c]$ gcc calculate-length-of-string.c -o calculate-length-of-string [sgupta@rhel54x64 c]$ ./calculate-length-of-string Enter the string you want to calculate length : http://saurabhgupta0527.blogspot.com Length of the Given String is : 36 [sgupta@rhel54x64 c]$ */
See also
Other popular tricky C Sample Codes and language Concept
.
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment