编写函数void count(char a[],char w[][10],int n,int b[])。
2017-08-06 18:58
621 查看
编写函数void count(char a[],char w[][10],int n,int b[])。
功能是:统计w指向的数组中的n个单词在a指向的字符串中各自出现的次数
将非字母字符看作单词分割符,拧将统计结果依次保存在b指向的数组中
#include <stdio.h>
#define N 4
int mystrlen(const char *str)
{
int length = 0;
while (*str++)
{
length++;
}
return length;
}
char* mystrstr(const char *str, const char *sub)
//在主串中查找子串,找到则返回首次出现的地址,否则返回NULL
{
int n = 0;
if (sub != NULL)
{
while (*str)
{
for (n = 0; (*(str + n) == *(sub + n)); n++)
{
if (*(sub + n + 1) == '\0')
{
return (char *)str;
}
}
str++;
}
return NULL;
}
else
{
return (char *)str;
}
}
void count(char a[], char w[][10], int n, int b[])
{
int i = 0;
int j = 0;
int count = 0;
char *p = a;
for (i = 0; i < n; i++)
{
p = mystrstr(p, w[i]); //在a中查找w[i]指向的单词
while (p != NULL)
{
count++;
p += mystrlen(w[i]);
p = mystrstr(p, w[i]); //找到之后计数器加一,并且将其出现次数放入b
}
b[j++] = count;
count = 0;
p = a;
}
}
void PrintArr(int *b, int n) //打印数组b
{
int i = 0;
for (i = 0; i < n; i++)
{
printf ("%d ", b[i]);
}
printf ("\n");
}
void PrintCarr(char w[][10], int n) //打印数组w
{
int i = 0;
for (i = 0;i < n; i++)
{
printf ("%s ", w[i]);
}
printf ("\n");
}
int main()
{
char word
[10] = {"hello", "world", "apple", "duck"}; //初始化w指向的单词
char a[100] = "lcchellowdkworldlcchellosdapplekducklikeduck"; //初始化a字符串
int b[10] = {0};
printf ("The original string is : \n");
printf ("%s\n", a);
printf ("The original words are :\n");
count(a, word, N, b);
PrintCarr(word, N); //打印原始单词
printf ("The results are : \n");
PrintArr(b, N); //打印单词出现的次数
return 0;
}
功能是:统计w指向的数组中的n个单词在a指向的字符串中各自出现的次数
将非字母字符看作单词分割符,拧将统计结果依次保存在b指向的数组中
#include <stdio.h>
#define N 4
int mystrlen(const char *str)
{
int length = 0;
while (*str++)
{
length++;
}
return length;
}
char* mystrstr(const char *str, const char *sub)
//在主串中查找子串,找到则返回首次出现的地址,否则返回NULL
{
int n = 0;
if (sub != NULL)
{
while (*str)
{
for (n = 0; (*(str + n) == *(sub + n)); n++)
{
if (*(sub + n + 1) == '\0')
{
return (char *)str;
}
}
str++;
}
return NULL;
}
else
{
return (char *)str;
}
}
void count(char a[], char w[][10], int n, int b[])
{
int i = 0;
int j = 0;
int count = 0;
char *p = a;
for (i = 0; i < n; i++)
{
p = mystrstr(p, w[i]); //在a中查找w[i]指向的单词
while (p != NULL)
{
count++;
p += mystrlen(w[i]);
p = mystrstr(p, w[i]); //找到之后计数器加一,并且将其出现次数放入b
}
b[j++] = count;
count = 0;
p = a;
}
}
void PrintArr(int *b, int n) //打印数组b
{
int i = 0;
for (i = 0; i < n; i++)
{
printf ("%d ", b[i]);
}
printf ("\n");
}
void PrintCarr(char w[][10], int n) //打印数组w
{
int i = 0;
for (i = 0;i < n; i++)
{
printf ("%s ", w[i]);
}
printf ("\n");
}
int main()
{
char word
[10] = {"hello", "world", "apple", "duck"}; //初始化w指向的单词
char a[100] = "lcchellowdkworldlcchellosdapplekducklikeduck"; //初始化a字符串
int b[10] = {0};
printf ("The original string is : \n");
printf ("%s\n", a);
printf ("The original words are :\n");
count(a, word, N, b);
PrintCarr(word, N); //打印原始单词
printf ("The results are : \n");
PrintArr(b, N); //打印单词出现的次数
return 0;
}
相关文章推荐
- 编写函数void count(char a[],char w[][10],int n,int b[])。功能是:统计w指向的数组中的n个单词在a指向的字符串中各自出现的次数(将非字母字符看作单词分
- 编写函数void count(char a[],char w[][10],int n,int b[])。功能是:统计w指向的数组中的n个单词在a指向的字符串中各自出现的次数(将非字母字符看作单词分
- 编写函数 void count(char a[],char w[][10],int n,int b[]).功能是:统计w指向的数组中的n个单词在a指向的字符串中各自出现的次数(将非字符字符看作单词分割
- 编写一个函数 int count_chars(char const *str,char const *chars) 函数应该在第一个参数中进行查找, 并返回匹配第二个参数所包含的字符的数量。
- 编写函数void change(char *a,char *b,char*c
- C语言编程练习5——编写函数void change(char *a,char *b,char*c)。 函数功能是首先把b指向的字符串逆向存放
- 编写函数void fun(char *s),其功能是将s所指的字符串逆序存放
- 实现内存复制函数void * memcpy(void *dst,void *src,int count).
- 不使用库函数,编写函数int strcmp(char *source, char *dest) 相等返回0,不等返回-1【转】
- 编写函数void change(char *a,char *b,char*c)。 函数功能是首先把b指向的字符串逆向存放,然后将a指向的字符串和b指向的字符串按排列顺序交叉合并到c指向的数组中,两个字
- 编写一个函数,删除字符串中的一段字符,函数原型为: int fun(char *p,int start,int end); 返回值为删除字符数, start 开始位置,end 结束位置。
- 不使用库函数,编写函数int strcmp(char *source, char *dest) 相等返回0,不等返回-1;
- 编写函数int count_number_string(char str[])和函数int maxnum_string(char str[])
- 不使用库函数,编写函数int strcmp(char *source, char *dest) 相等返回0,不等返回-1;
- 】编写void put(int n)函数时,注意考虑数组满的情况
- 编写函数void change(char *a,char *b,char*c)。 函数功能是首先把b指向的字符串逆向存放,然后将a指向的字符串和b指向的字符串按排列顺序交叉合并到c指向的数组中,两个字
- 编写函数 void dollars(char *dest,char const *src)
- 编写函数void change(char *a,char *b,char*c)。 函数功能是首先把b指向的字符串逆向存放 然后将a指向的字符串和b指向的字符串按排列顺序交叉合并到c指向的数
- Linux下的C语言编程——编写函数void change(char *a,char *b,char*c)
- 不使用库函数,编写函数int strcmp(char *source, char *dest) 相等返回0,不等返回-1