Home C-tutorials C String length



C String length
Posted on: February 5, 2009 at 12:00 AM
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:

Related Tags for C String length:
cstringiovariablermiriathislengthexampletoinilearnexameareilitsectioncanliinitvarinitializeinrmmtrmincamincldeclarehowxaxampstermeeishallivmpleaandarstrvazssriringthavstabablecletermeterminitialpleplmindono


More Tutorials from this section

Ask Questions?    Discuss: C String length   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.