len of str vbs
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, world!";
int len = strlen(str);
printf("Length of the string: %d", len);
return 0;
}
Explanation:
#include <stdio.h>
and#include <string.h>
: These lines include necessary header files for standard input/output functions (stdio.h
) and string functions (string.h
).int main() { ... }
: This is the main function where the program execution begins and contains the code for finding the length of a string.char str[] = "Hello, world!";
: Defines a character array namedstr
and initializes it with the string "Hello, world!".int len = strlen(str);
: Declares an integer variablelen
and assigns it the value returned bystrlen()
function.strlen()
calculates the length of the string stored in the variablestr
.printf("Length of the string: %d", len);
: Prints the length of the string usingprintf()
function with a format specifier%d
to display the integer value stored in the variablelen
.return 0;
: Indicates the end of themain()
function and returns 0, indicating successful execution of the program to the operating system.