finding characters in string

#include <stdio.h>

int main() {
    char str[] = "Hello, world!";
    char target = 'o';
    int count = 0;

    for (int i = 0; str[i] != '\0'; i++) {
        if (str[i] == target) {
            count++;
        }
    }

    printf("The character '%c' appears %d times in the string.\n", target, count);

    return 0;
}

Explanation:

  1. Include Header File: The #include <stdio.h> directive is used to include the standard input-output header file in the program.

  2. Main Function: The main() function is the entry point of the program execution.

  3. Declare Variables:

  4. char str[] = "Hello, world!"; declares and initializes a character array str containing the string "Hello, world!".
  5. char target = 'o'; declares a character variable target and assigns the value 'o' to it.
  6. int count = 0; initializes an integer variable count to store the count of occurrences of the target character.

  7. For Loop:

  8. for (int i = 0; str[i] != '\0'; i++) initializes i to 0 and iterates through the string str until it encounters the null character ('\0') denoting the end of the string.
  9. Inside the loop:

    • if (str[i] == target) checks if the current character in the string (str[i]) is equal to the target character ('o' in this case).
    • If true, count is incremented by 1 using count++.
  10. Print Result:

  11. printf("The character '%c' appears %d times in the string.\n", target, count); prints the count of occurrences of the target character.

  12. Return: The return 0; statement signifies the successful execution of the program, and 0 is returned to the operating system to indicate this. ```