Example when program is run, string which its value/content is "abc" is counted 3 characters, string "Love Mom" is conted 8 characters. Syntax is used as follows:
printf("%d", strlen(name_of_string));
Example of program to count amount of character of string value/content on C programming language:
printf("%d", strlen(name_of_string));
Example of program to count amount of character of string value/content on C programming language:
- Open Dev C++
- File menu > New > Source File, then will be created a new worksheet titled Untitled1
- File menu > Save As. On Save as type: choose C source files (*.c) and on File name, example we give name count string character.c
- First, type minimal required syntax on C programming:
main()
{
} - Add getch() function in main() function to show program result while be Run:
main()
{
getch();
} - Define a string variable, and directly define its value/content
main()
{
char name_of_string[] = "X";
getch();
} - To show amount of character of the string value/content, use syntax printf("%d", strlen(nama_string))
main()
{
char name_of_string[] = "X";
printf("%d", strlen(name_of_string));
getch();
}
Please Compile&Run, then on Run result will show 1, because X is consist of 1 character, if we change with Love Mom:
main()
{
char name_of_string[] = "Love Mom";
printf("%d", strlen(name_of_string));
getch();
}
then on Run result will show 8, because word "Love Mom" is consist of 10 characters, (7 letters and 1 space), please try! - For more interesting can be added sentence:
main()
{
char name_of_string[] = "Love Mom";
printf("Word %s is consist of %d characters", name_of_string, strlen(name_of_string));
getch();
}
0 komentar:
Posting Komentar