OCJP

String as an Array in C

In C Language, String is an array of Char which is terminated by ‘\0’ (NULL).

#include<stdio.h>
#include<conio.h>

void main()
{
	char nm[12];//String
	int i;
	clrscr();
	printf("\nEnter Name :");
	scanf("%s",&nm);

	i=0;
	while(nm[i]!= '\0') //NULL
	{
	printf("\nAt nm[%d] char is %c",i,nm[i]);
	i++;
	}
	printf("\nLength of String %d",i);
	getch();
}
String (Array of Char) in C

When you iterate the loop up to NULL character then I will be the length of the String.

Leave a Reply

Your email address will not be published. Required fields are marked *


× How can I help you?