Example: Write fun, which finds the number of specified characters in the string to which STR refers and returns this value.

For example, if the string asdfasdf is entered and the character a is entered, the output is 2. Do not change anything in main or the other functions; just put some statements you wrote in curly braces for fun.

The code is as follows:

#include<conio.h>
#include<stdio.h>
#include<string.h>
#define N 100
int fun(char*str,char c)
{
	int n=0;
	while(*str)
	{
		if(*str==c)
		n++;
		str++;
	}
	return n;
}
main()
{
	char s[N],ch;
	FILE*out;
	printf("\nPlease enter a string:");
	gets(s);
	printf("\nPlease enter a char:");
	ch=getchar();
	printf("\nThe number of the char is:%d\n",fun(s,ch));
	out=fopen("outfile.dat"."w");
	strcpy(s,"The number of the char is:");
	fprintf(out,"%d",fun(s,'a'));
	fclose(out);
}
Copy the code

The output running window is as follows:



Other exercises this week

C programming column

① Please write the function fun, its function is: STR in the string of even subscript characters deleted, the remainder of the string formed by the new string is placed in the array of S.

② The results of N students have been put into a linked list structure of the head node in the main function. A points to the head node of the linked list. Write a function called fun, which finds the student’s highest score and returns it.

③ Assume that the input string contains only letters and “#” signs. Write fun, which moves all leading “#” signs to the end of the string.

C programming > 11 week ④ Please write the function fun, the function is: determine whether the string is palindrome? If it returns 1, the main function prints YES, otherwise it returns 0, and the main function prints NO. Palindromes are strings that are read the same backwards and forwards.

⑤ Write a function to remove all Spaces from the string.

⑥ A student’s record consists of a student number, 5 course scores and average scores. The student number and 5 course scores are given in the main function. Write a function called fun, whose function is to find the average score of the student and put it in the recorded AVE members.

Find the number of characters STR refers to in the string, and return this value.

If there are m integers in the array, it is required to move the array elements with subscripts from 0 to t(t≤m-1) to the end of the array.

The harder you work, the luckier you get! Come on, Aoli!!