int main() {
//字符
//字符串 ATM
//是否继续?(y/n):
//名字 语句 默认排序(a-z)
char c = 'b';
printf("%d\n",c);
int a = 65;
printf("%c\n",a);
/**
Int char
ASCII码表 2 3 4 5 6 7 8 9 10 J Q K A
*/
for (int i = 65; i < 91; ++i) {
printf("%c ", i);
}
printf("\n");
for (int i = 97; i < 123; ++i) {
printf("%c ", i);
}
printf("\n");
/**
从终端输入一段语句 Hi you are so beautiful!
数组名就是这个数组的首地址(指针)
使用%s接收终端输入的字符串时注意
1.遇到空格就表示这个字符串结束
2.遇到\n表示字符串结束
终端输入的顺序
1.先从操作系统的缓存中查找有没有需要的字符串
2.如果缓存中没有 就等待用户从终端输入
3.如果缓存中有 就直接从缓存中读取
getChar():从终端获取一个字符 (注意:每次输入的\n字符)
*/
/*
char sentence[50] = {};
scanf("%s", sentence);
printf("%s\n", sentence);
char temp[20] = {};
scanf("%s", temp);
printf("%s", temp);
*/
/**
hello merry\n
*/
//终端输入一个字符
//getchar();
//getchar();
char sentence[50] = {};
char temp;
int i = 0;
while (1){
temp = getchar();
if (temp == '\n'){
break;
}
sentence[i] = temp;
i++;
}
printf("%s\n", sentence);
/**
a.终端输入一串字符串 计算单词的个数 计算某个单词出现的次数
b.输入多个学生的姓名(英文) 对姓名从a-z的顺序排序
*/
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
/**
查找单词的个数
查找某个单词出现的次数
Hi!How are you ?
@zhangsan:nice day!
*/
int main() {
char sentence[50] = {}; // \0 \0 \0
//从终端输入一串字符串
for (int i = 0; i < 50; ++i) {
char temp = getchar();
if (temp == '\n'){
break;
}
sentence[i] = temp;
}
//获取字符串中字符的个数
int num = strlen(sentence);
//查找单词个数
//i 控制从头 到尾 的遍历
char temp;
bool isWordStart = false;
int wordCount = 0;
for (int i = 0; i < num; ++i) {
//获取i对应的字符
temp = sentence[i];
//判断是不是字母
if ((temp >= 'A' && temp <= 'Z') || (temp >= 'a' && temp <='z')){
//是一个字母
//判断是不是单词的开头
if (isWordStart == false){
//这个字母就是单词的开头
isWordStart = true;
}
}else{
//不是字母
//判断上一次是不是单词
if (isWordStart == true){
wordCount++;
isWordStart = false;
}
}
}
printf("单词个数为:%d\n", wordCount);
return 0;
}
但是这样的话,若句后无空格则不计入
eg.how are you 为2 how are you?为3
所以我们可以把wordCount++提到
判断字母开头之后,即一判断到是字母开头就+1;
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
/**
查找单词的个数
查找某个单词出现的次数
Hi!How are you ?
@zhangsan:nice day!
*/
int main() {
char sentence[50] = {}; // \0 \0 \0
//从终端输入一串字符串
for (int i = 0; i < 50; ++i) {
char temp = getchar();
if (temp == '\n'){
break;
}
sentence[i] = temp;
}
//获取字符串中字符的个数
int num = strlen(sentence);
//查找单词个数
//i 控制从头 到尾 的遍历
char temp;
bool isWordStart = false;
int wordCount = 0;
for (int i = 0; i < num; ++i) {
//获取i对应的字符
temp = sentence[i];
//判断是不是字母
if ((temp >= 'A' && temp <= 'Z') || (temp >= 'a' && temp <='z')){
//是一个字母
//判断是不是单词的开头
if (isWordStart == false){
//这个字母就是单词的开头
isWordStart = true;
wordCount++;
}
}else{
//不是字母
//判断上一次是不是单词
if (isWordStart == true){
isWordStart = false;
}
}
}
printf("单词个数为:%d\n", wordCount);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
/**
常量字符串 (定义之后不能改变)内存由系统自动分配
char *sentence = "xiaowang";
从终端输入 或者是 能够改变
char name[40] = {};
*/
int main() {
//常量字符串 (定义之后不能改变)内存由系统自动分配
//char *sentence = "I’m going out shopping , and won’t be back until about 5:00 pm. I have taken with me the two books you asked me to return to the city library .";
char *sentence = "jack jackSon nijack wojack";
printf("%s\n", sentence);
//接收用户的输入
char word[20] = {};
scanf("%s", word);
//计算语句和单词中字符的个数
int sentence_num = strlen(sentence);
int word_num = strlen(word);
bool isWordStart = false;
char temp;
int total = 0;
//查找
for (int i = 0; i < sentence_num; ++i) {
//查找单词的开头
temp = sentence[i];
//判断是不是字母
if ((temp >= 'A' && temp <= 'Z') || (temp >= 'a' && temp <= 'z') ){
//判断是不是开头
if (isWordStart == false){
isWordStart = true;
//查找这个单词和输入的单词是否相同
int j = 0;
for (; j < word_num; ++j) {
if (word[j] != sentence[i+j]){
break;
}
}
//判断跳出循环的方式
if (j == word_num){
//判断是不是遍历结束了
if (i + j == sentence_num){
//最后一个单词和当前的这个单词相同
total++;
}else{
//判断后面一个字符是不是字母
if (sentence[i+j] < 'A' || (sentence[i+j] >'Z' && sentence[i+j] < 'a')
|| sentence[i+j] > 'z'){
//单词存在
total++;
}
}
//判断sentence的i+j后面的哪个是不是字母
}
}
}else{
isWordStart = false;
}
}
printf("单词出现的次数:%d\n", total);
return 0;
}