char str[] = "HELLO WORLD";
it will be stored in the memory like this:
H
|
E
|
L
|
L
|
O
|
W
|
O
|
R
|
L
|
D
|
\0
|
String Length Without Using Library Function:
#include <stdio.h>
int main()
{
char str[1000];
int i, count = 0;
printf("Enter a string: ");
gets(str);
for(i=0;i<1000;i++) {
if(str[i] == '\0')
break;
count++;
}
printf("There are %d characters in the given string.", count);
return 0;
}
String Length using Library Function:
There is actually a library function to get the string length. The function is strlen(char *) and this function is in the header file string.h. Here is an example:
#include <stdio.h>
#include <string.h>
int main()
{
char str[1000];
int len;
printf("Enter a string: ");
gets(str);
len = strlen(str);
printf("There are %d characters in the given string.", len);
return 0;
}
good job man . wish you best of luck
ReplyDelete