字符串输入

一、分配空间

  • 最简单的方法显式指明数组的大小

    char name[81]
    

二、get函数和fgets()函数

1.gets函数读取整行输入,直到遇到换行符,然后丢弃换行符,存储其他字符,并在字符的末尾添加一个空字符使其成为一个C字符串,它经常和puts()函数配对使用

2.fgets()函数有三个参数,第二个参数指明读入字符的最大数量。第三个参数指明要输入的文件。如果读入从键盘输入的数据,则以stdin(标准输入)作为参数,该标识符定义在stdio.h中,如果要显示在计算器上,应该使用stdout(标准输出)作为参数

  • 相同点
    • 两个函数都会一次读取一整行输入
    • gets()函数会在字符末尾添加一个空字符,使其成为字符串。fgets()会在长度超过限制的时候添加一个空字符在后面
  • 不同点
    • gets()函数会丢弃换行符,而fgets()函数不会添加换行符,但是会在读到换行符的时候进行保留
    • gets()函数不会检查数组是否装的下,它只直到数组开始处,并不知道数组结束处。如果数组过长,会导致缓存溢出,所以C11已将这个函数废除。fgets()函数则通过第二个参数来控制读取字符串的长度。比较安全。
    • gets()函数没有返回值。fgets()函数有返回值,返回char的指针。1.如果读取过程顺利,会返回传入第一个参数的地址 2.如果读到文件末尾,将返回空指针(NULL),读取错误的时候也返回空指针。
  • fgets()优缺点
    • 缺点是保留了换行符
    • 好处是可以检查末尾是否有换行符来判断是否读取了一行
    • 如果超过了长度n,fgets()函数会在末尾添加一个空字符'\0'
#include <stdio.h>
#define STLEN 81
int main(void){
    char words[STLEN];

    puts("Enter a string,please");
    gets(words);
    printf("Your string twice:\n");
    printf("%s\n",words);
    puts(words);//puts()函数会在字符串末尾添加换行符
    puts("Done");
    return 0;
}
运行结果
Enter a string,please
warning: this program uses gets(), which is unsafe.
I want to learn about string theory
Your string twice:
I want to learn about string theory
I want to learn about string theory
Done

进程已结束,退出代码0
#include <stdio.h>
#define STLEN 14
int main(void){
    char words[STLEN];

    puts("Enter a string ,please");
    fgets(words,STLEN,stdin);//这边输入的长度比第二个参数短,保留了换行符
    printf("Your string twice (puts(),then fputs()):\n");
    puts(words);//在上面的基础上又添加了换行符
    fputs(words,stdout);
    puts("Enter another string ,Please");
    fgets(words,STLEN,stdin);//输入的字符串比第二个参数长,所以只截取了一部分,里面没有换行符
    puts(words);
    fputs(words,stdout);
    puts("Done");
    return 0;
}
运行结果
Enter a string ,please
apple pie
Your string twice (puts(),then fputs()):
apple pie

apple pie
Enter another string ,Please
strawberry shortcake
strawberry sh
strawberry shDone
#include <stdio.h>
#define STLEN 10
int main(void){
    char words[STLEN];

    puts("Enter strings(empty line to quit):");
    /**
     * 先读取"y,the ge"并存储为"有,the ge\0"......在用户按下Return键之前,输入都被存储在缓冲区
     */
    while(fgets(words,STLEN,stdin)!=NULL&&words[0]!='\n')//第二个条件是为了防止空行的情况(即首字符是换行符)
        fputs(words,stdout);
    return 0;
}
运行结果
Enter strings(empty line to quit):
By the way,the gets() function
By the way,the gets() function
also return a null pointer if it
also return a null pointer if it
encounters end-of-file
encounters end-of-file


进程已结束,退出代码0
#include <stdio.h>
#define STLEN 10

int main(void){
    char words[STLEN];
    int i;

    puts("Enter strings(empty line to quit):");
    /*
     * fgets()函数会每次读取限制长度以后会在后面加一个'\0'
     */
    while(fgets(words,STLEN,stdin)!=NULL&&words[0]!='\n'){//第二个条件是为了防止首字母为空,是空行
        i=0;
        while(words[i]!='\n' && words[i]!='\0')
            i++;
        if(words[i]=='\n')
            words[i] = '\0';
        else//如果words[i] =='\0'则执行这部分代码
            while (getchar() != '\n')
                continue;
        puts(words);
    }
    return 0;
}
运行结果
Enter strings(empty line to quit):
This
This
program seems
program s
unwilling to accept long lines
unwilling
But it doesn't get stuck on long
But it do
lines either
lines eit


进程已结束,退出代码0

三、gets_s()函数

  • C11新增gets_s函数和fgets()函数类似
  • gets_s()只从标准输入中读取数据,所以不需要第三个参数
  • gets_s()读到换行符,会丢弃它而不是存储它
  • 如果gets_s()函数读到最大字符数都没有读到换行符
    • 会将目标数组中的首字符设置为空字符
    • 读取并丢弃随后的输入直到读到换行符或文件结尾,然后返回空指针
char *s_gets(char *st,int n){
    char * ret_val;
    int i=0;
    /**
     * 如果函数读到文件结尾,将返回空字符
     */
    ret_val =fgets(st,n,stdin);
    if(ret_val){//即ret_val!=NULL
        while(st[i]!=NULL&&st[i]!='\0')
            i++;
        if(st[i]=='\n')
            st[i] ='\0';
        else
            while(getchar()!='\n')
                continue;
    }
    return ret_val;
}

四、strlen()函数

  • strlen()函数用于统计字符串的长度,可以用来缩减字符串的长度
void fit(char *string,unsigned int size){
    if(strlen(string) >size){
        string[size] ='\0';
    }
}

五、strcat 和strncat

  • 相同点
    • 都是用来拼接字符串,都是将第二个字符串拼接到第一个字符串的末尾
  • 不同点
    • 如果是strcat()函数是将第二个字符串复制到数组中,有可能第二个字符串的长度大于数组的长度,从而产生溢出,导致不安全。strncat函数则用第三个参数,指定拷贝的长度,这就把决定权交到程序员手里,由程序员控制风险。
  • 注意点
    • 使用strncat函数计算字符串长度时,要注意拼接字符串末尾还有一个'\0',所以长度要加1

六、strcmp和strncmp

  • 相同点
    • 把用户输入的字符串和已经存在的字符串进行比较
    • 两个字符串相同,就返回0,否则返回非0值,第一个字符在第二个字符前面,strcmp()函数就返回负数,反之,strcmp()则返回正数
    • 都会依次比较每个字符,直到发现第1对不同的字符为止,比较的是所有的字符,不是字母
  • 不同点
    • strcmp()函数会比较字符串中的字符,直到发现不同的字符为止,会持续到字符串的末尾,而strncmp()函数可以指定比较多少个字符不一样为止

七、strcpy()函数和strncpy()函数

和strcat()函数有些类似,都是字符串的拷贝,strcat类似于字符串的拼接操作,即将第二个字符串拼接到第一个字符串的后面。strcpy则是直接拷贝到目标数组中去,要注意的是,如果拷贝长度超过,strcpy()不会在字符串后面添加空字符,需要自己手动设置。

posted @ 2023-07-08 23:50  SHG4666  阅读(56)  评论(0)    收藏  举报
顶部