字符数组char s[31]用于保存不超过30个字符的字符串,利用指针编程,统计s[31]中给定字符的个数。要求用函数实现功能,主程序只完成数据的输入输出。

被调用函数原型:void CountChar(char *str,  char ch,int *counter),str是指向字符串的指针,ch是被统计的字符,counter是保存字符ch个数的指针。

输入提示信息:"Input string:\n"

                         "Character to be counted:\n"

输入格式:    用gets()函数

输出格式:"The numbers of %c is %d.\n"

运行样例

Input string:

you are my sunshine.

Character to be counted:

e

The numbers of e is 2.
 

#include<stdio.h>
void countchar(char*str, char ch, int *counter);
int main()
{
	char s[31];
	char ch;
	int counter=0;
	printf("input string:\n"); 
	gets(s);
	printf("character to be counter:\n");
	ch = getchar();
	countchar(s,ch,&counter);
	printf("the number of %c is %d.\n",ch,counter);
	return 0;
 }
void countchar(char *str,char ch, int *counter)
{
	int i;
	for(i=0;str[i]!='\0';i++)
	{
		if(str[i]==ch)
			(*counter)++;
	}
 } 
 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用冒泡排序或快速排序等算法对字符指针数组进行排序,具体实现如下: 1. 定义一个字符指针数组,存储5个字符串。 2. 使用冒泡排序或快速排序等算法对字符指针数组进行排序。 3. 输出排序后的字符指针数组。 示例代码如下: ```c #include <stdio.h> #include <string.h> #define MAX_LEN 20 void bubble_sort(char *arr[], int n); void quick_sort(char *arr[], int left, int right); int partition(char *arr[], int left, int right); int main() { char *countries[] = {"China", "USA", "Japan", "Germany", "France"}; int n = sizeof(countries) / sizeof(char *); // 使用冒泡排序对字符指针数组进行排序 bubble_sort(countries, n); // 使用快速排序对字符指针数组进行排序 // quick_sort(countries, 0, n - 1); // 输出排序后的字符指针数组 for (int i = 0; i < n; i++) { printf("%s\n", countries[i]); } return 0; } // 冒泡排序 void bubble_sort(char *arr[], int n) { for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (strcmp(arr[j], arr[j + 1]) > 0) { char *temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } // 快速排序 void quick_sort(char *arr[], int left, int right) { if (left < right) { int pivot = partition(arr, left, right); quick_sort(arr, left, pivot - 1); quick_sort(arr, pivot + 1, right); } } int partition(char *arr[], int left, int right) { char *pivot = arr[right]; int i = left - 1; for (int j = left; j < right; j++) { if (strcmp(arr[j], pivot) < 0) { i++; char *temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } char *temp = arr[i + 1]; arr[i + 1] = arr[right]; arr[right] = temp; return i + 1; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值