C语言 将一个字符串中的单词提取到另一个数组中。并显示数量

#include<stdio.h>
#include<string.h>
#define N 1000
#define M 1000
void main()
{
char a[N],b[M][N];
int n,j=0,num=0,i,word=0;
gets(a);
n=strlen(a); // n为a[N]的长度
for(i=0;i<n;i++)
{
if((a[i]>64&&a[i]<91)||(a[i]>96&&a[i]<123))
{
word=1;
strcpy(b[j],a[i]);
}
else
{
num++;
j++;
}
}
}

BUG是cannot convert parameter 2 from 'char' to 'const char *'
求详细解释。谢谢大神

strcpy的两个参数都要是一维数组的形式,你给的两个参数一个是一维char数组,一个是char,两个类型不匹配,所以出错。

给你写了个参考

#include<stdio.h>
#include<string.h>
#define N 1000
#define M 1000
int main()
{
    char a[N], b[M][N], temp[N];
    int n,num=0,i,word=0;
    gets(a);
    n=strlen(a); // n为a[N]的长度
    for(i=0;i<n;i++)
    {
        if(a[i] == ' ' || a[i] == '\t')//单词分隔符
        {
            if (word != 0){
                temp[word] = '\0';
                strcpy(b[num],temp);
                num++;
                word = 0;
            }
        }
        else
        {
            temp[word++] = a[i];//
        }
    }
    if (word != 0){//最后一个单词后面可能没有分隔符
        temp[word] = '\0';
        strcpy(b[num], temp);
        num++;
        word = 0;
    }
    printf ("num:%d\n", num);
    for (i = 0; i < num; ++i){
        printf ("%s\n", b[i]);
    }
return 0;
}

温馨提示:答案为网友推荐,仅供参考
相似回答