可采纳
11 、13行
c2=‘ ’
if语句里面写错了吧
你直接判断空
可参考
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int count = 0;
int i = 0;
printf("请输入一行字符:");
scanf("%s", str);
while (str[i] != '\0') {
if (str[i] == ' ') {
count++;
} else {
i++;
}
}
printf("单词数:%d", count);
return 0;
}
#define MAX_VERTEX_NUM 20 //图的邻接表存储表示
typedef struct ArcNode{
int adjvex; //该弧所指向的顶点的位置
struct ArcNode *nextarc; //指向下一条弧的指针
InfoType *info; //该弧相关信息的指针
}ArcNode;
typedef struct VNode {
VertexType data; //顶点信息
ArcNode *firstarc; //指向第一条依附该顶点弧的指针
}VNode,AdjList[MAX_VERTEX_NUM]
Typedef struct {
AdjList vertices;
int vexnum,arcnum; //图的当前顶点数和弧数
int kind; //图的种类标志
}ALGraph;
题主代码修改如下,供参考:
#include <stdio.h>
int main()
{
char str[80];
int cnt = 0, i = 0, flg = 0;
printf("Please input a string:\n");
gets(str);
while (str[i] != '\0') {
if (str[i] != ' ') {
if (!flg)
cnt++;
flg = 1;
}
else
flg = 0;
i++;
}
printf("单词数:%d", cnt);
return 0;
}