C - 基础 - 字符串函数

℡╲_俬逩灬. 提交于 2019-12-21 13:16:08

字符串输入

gets函数,构造为 char *gets(char * s) , 不足是不检查预留存储区是否能够容纳你实际输入数据,多出则溢出。

#include <stdio.h>
#define MAX 80

int main()
{
    char name[MAX];
    char *ptr;

    printf("What's your name?\n");
    ptr = gets(name);
    printf("Hello , %s!\n" , name);

    return 0;
}

fget函数,避免溢出问题,第二个参数指定获取的长度,第三个参数为获取来源,可以是文件或标准输入。

#include <stdio.h>
#define MAX 10

int main()
{
    char name[MAX];
    char *ptr;

    printf("What's your name?\n");
    ptr = fgets(name, MAX, stdin);
    printf("Hello , %s! , my firend %s!\n", name, ptr);

    return 0;
}

scanf函数,优点是可以格式化输入,缺点是前一个未能读完的参数会成为第二个变量值。

#include <stdio.h>
#define MAX 10

int main()
{
    char name[11], gf_name[12];
    int count;

    printf("What's your name and your gf's name?\n");
    count = scanf("%5s %10s", name, gf_name);
    printf("Hello %d, , %s! , your girl friend is %s!\n", count, name, gf_name);

    return 0;
}

字符串输出

  1. puts 直接输出
  2. fputs 可输出到文件流
  3. priintf 格式化输出
#include <stdio.h>

int main()
{
    char beauty[80] = "you are best beautiful in the world";
    const hi = "ni hao a xiaohui !";

    puts("this is puts call\n");

    fputs(beauty, stdout);
    puts("\n");

    printf("foramt output is %s \n", "ok");
    return 0;
}

常用字符串函数

#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
    char beauty[80] = "you are best beautiful in the world\n";
    char hi[120] = "ni hao a xiaohui !";
    // strlen 函数 字符个数,不包括 结束标志
    printf("length of beauty is %d , sizeof is %d \n", strlen(beauty), sizeof(beauty));
    *(beauty + 3) = 'r';// 修改字符串
    printf(beauty);
    // strcat(s1,s2) 向s1添加s2 , s1可能溢出
    printf("strcat add = %s \n", strcat(hi, beauty));
    // strncat(s1,s2,max) 向s1添加s2 指定添加的长度
    char s1[5] = "hi";
    char s2[10] = "hi boy";
    printf("strncat add = %s \n", strncat(s1, s2, 2)); // hihi
    // strcmp(s1,s2) 相同则返回0
    char s3[3] = "aa";
    char s4[4] = "aa";
    printf("s3==s4 is %d , strcmp(s3,s4) is %d \n", s3 == s4, strcmp(s3, s4));
    // strncmp(s1,s2,length) 对字符串前len个字符进行比较
    char s5[3] = "aa";
    char s6[4] = "aab";
    printf("s5==s6 is %d , strcmp(s5,s6) is %d ,strcmp(s5,s6) is %d \n", \
        s5 == s6, strcmp(s5, s6), strncmp(s5, s6, 2));
    // strcpy(target,source) 字符串拷贝 ,不检查目标字符串是否溢出
    char temp[50];
    char greet[15] = "good morning!";
    strcpy(temp, greet);
    strcpy(temp + strlen(greet), "ok"); // 再追加
    printf("after strcpy temp is: %s \n", temp);
    // strncpy(target,source,n)  检查目标字符串溢出
    char target[10];
    char source[30] = "what is make you so beautiful";
    strncpy(target, source, sizeof(target) - 1);
    target[sizeof(target) - 1] = '\0'; // 源字符串超出限制,还要手动补 \0
    printf("after strncpy target is: %s \n", target);
    // sprintf(target,format,arg1,arg2...) 格式化输出到字符串
    char brief[50];
    sprintf(brief, "I am %d years old ,my name is %s", 24, "qianqian.sun");
    printf("after sprintf brief is: %s \n", brief);
    // 其他字符串函数
    // char *strchr(const char * s , int c); 返回一个指向s中存放字符c的第一个位置的指针,若没有找到,返回空指针
    // cahr *strpbrk(const char * s1 ,const char * s2); 返回一个指针,指向s1中存放s2中任何字符串的第一个位置。
    // char *strrchr(const char * s ,int c);返回一个指针,指向字符串s中字符c最后一次出现的地方;
    // cahr *strstr(const char * s1,const char * s2);返回指针,指向s1中第一次出现s2的地方。
    // atoi(char * s) 字符串转整数
    // atof() atol() strtol() strtoul() strtod()字符串转换为数字 使用sprintf转换为字符串
    return 0;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!