Home C-tutorials C String length

Related Tutorials


 
 

Share on Google+Share on Google+

C String length

Advertisement
In this section, you will learn how to determine the length of a String in C. You can see in the given example, we have declared a string and initialize a variable i to 0.

C String length

     

In this section, you will learn how to determine the length of a String in C. You can see in the given example, we have declared a string and initialize a variable i to 0. The while loop will increment the value of i till it reach the string terminating character or up to the last letter of the string value.

Here is the code:

StringLen.c

#include <stdio.h>
#include <conio.h>
void main() {
  char str[] "Hello World";
  int i=0;
  while (str[i!= 0)
 
  i++;
  printf("\nThe length of the string \"%s\" is %d.", str, i);
  getch();
}

Output will be displayed as:

Download Source Code:

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 5, 2009

Related Tutorials

Discuss: C String length   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Gelldur
November 25, 2011
Better Code

You should write: while(str[i++] != 0); It looks better!
DMCA.com