Rabu, Mei 28, 2014

Count Amount of Character of String Value/Content on C Programming Language

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:
  1. Open Dev C++
  2. File menu > New > Source File, then will be created a new worksheet titled Untitled1
  3. 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
  4. First, type minimal required syntax on C programming:

    main()
    {
    }


  5. Add getch() function in main() function to show program result while be Run:

    main()
    {
      getch();
    }


  6. Define a string variable, and directly define its value/content

    main()
    {
      char name_of_string[] = "X";
      getch();
    }


  7. 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!
  8. 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();
    }

Thank you... : )

0 komentar: