您好,欢迎来到华佗健康网。
搜索
您的当前位置:首页C语言:字符数组相关操作

C语言:字符数组相关操作

来源:华佗健康网

字符串的基础操作

在用格式说明符%s进行输入输出时,其输入输出项均为数组名。但在输入时,相邻两个字符串之间 要用空格分隔,系统将自动的在字符串最后加结束符"\0"。在输出时,遇到结束符'\0'作为输出结 束标志。

对于字符串的操作,我们需要使用到一些系统提供的函数。

字符串输入

scanf("%s",数组名);

#include <stdio.h>
/**
 * 字符串的输入
 * 语法:scanf("%s",数组名);
 */ 
int test1()
{
    // 创建一个char数组,用来存放人的名字
    char name[20];
    printf("请输入您的姓名:\n");
    scanf("%s",name);// 注意:如果是字符串,这里不是变量地址,是变量名,数组本身不占内存空间,数组默
认指向第一个元素的首地址
    printf("您的姓名是%s\n",name);
}
int main(int argc,char *argv[])
{
    test1();
    return 0;
}

注意:采用scanf()进行字符串输入,要求字符串中不能存在空格,否则字符串遇到空格会结束。

fgets(数组名,数组容量,stdin);

说明:

采用fgets进行字符串输入,可获取所有输入的字符串,包含 \n ,在实际的字符串处理时,我 们可能需要处理 \n

#include <stdio.h>
/**
 * 字符串的输入
 * 语法:fgets(数组名,数组容量,sdtin);
 */
int test2()
{
    // 创建一个char数组,用来存放人的名字
    char name[20];
 
 printf("请输入您的名字:\n");
 
 // fgets和scanf只能二选一
 fgets(name,sizeof(name)/sizeof(char),stdin);
 
 printf("您的姓名是%s\n",name);
}
int main(int argc,char *argv[])
{
    test2();
    return 0;
}

注意:

① 如果输入的字符串不包括 空格 和 换行 ,可以使用scanf() | fgets();

② 如果输入的字符串需要包含 空格 和 换行 ,只能使用fgets();

③ 经过对比,我们发现,在字符串的输入中,fgets();

字符串输出

printf("%s",数组名);

#include <stdio.h>
/**
 * 字符串的输出
 * 语法:printf("%s",数组名);
 */ 
int test3()
{
    // 创建一个char数组,用来存放人的名字
    char name[20];
    printf("请输入您的姓名:\n");
    scanf("%s",name);// 注意:如果是字符串,这里不是变量地址,是变量名,数组本身不占内存空间,数组默
认指向第一个元素的首地址
    printf("您的姓名是%s\n",name);
}
int main(int argc,char *argv[])
{
    test3();
    return 0;
}

puts(数组名)

功能: 输出一个字符串

说明: 字符串可以包含转义字符

#include <stdio.h>
/**
 * 字符串的输入
 * 语法:puts(数组名);
 */
int test4()
{
    // 创建一个char数组,用来存放人的名字
    char name[20];
 
 printf("请输入您的名字:\n");
 
 // fgets和scanf只能二选一
 fgets(name,sizeof(name)/sizeof(char),stdin);
 
 puts(name); // 无法进行拼接,只能输出
 
 printf("您的姓名是%s\n",name);
}
int main(int argc,char *argv[])
{
    test4();
    return 0;
}

字符串拼接

strcat(数组名(字符串1),"需要拼接的字符串(字符串2)");

引用: #include <string.h>

#include <stdio.h>
#include <string.h>
/**
 * 字符串的拼接
 * 语法:strcat(数组名,"需要拼接的字符串");
 */
int test5()
{
    // 创建一个char数组,用来存放人的名字
    char name[20];
 
 printf("请输入您的名字:\n");
 
 // fgets和scanf只能二选一
 fgets(name,sizeof(name)/sizeof(char),stdin);
 
 puts(strcat(name," 快跑!")); // 无法进行拼接,只能输出
}
int main(int argc,char *argv[])
{
    test5();
   return 0;
}

注意:

① 字符数组1(字符串1) 的长度必须足够大,以便于能容纳被连接的字符串。

② 连接后系统将自动取消 字符串1(字符数组1) 后面的结束符'\0'

③ 字符串2 可以是字符数组名,也可以是字符串常量,如:strcat(s1,"def");

字符串拷贝

strcpy(数组名,字符串);

引入: #include <string.h>

说明: 这个函数适合给字符串赋值用

字符串比较

strcmp(字符串1,字符串2);

引入: #include <string.h>

功能: 比较两个字符串对应位置字符ASCII的大小(字典顺序比较)。

返回值:

        若字符串1等于字符串2,返回0

        若字符串1大于字符串2,返回正数

        若字符串1小于字符串2,返回负数

说明:

① 执行这个函数时,自左到右逐个比较对应字符的ASCII的值,直到发现了不同字符或字符串结束 符‘\0’为止。

② 对字符串不能用数值型比较符

③ 字符串1与字符串2可以是字符数组名,也可以是字符串常量

#include <stdio.h>
#include <string.h>
/**
 * 字符串的比较
 * 语法:strcmp(字符串1,字符串2);
 */
int test7()
{
    // 创建一个char数组,用来存放账户和密码
 char username[20],password[8];
 printf("请输入您的账户:\n");
 scanf("%s",username);
 printf("请输入您的密码:\n");
 scanf("%s",password);
 
 if(!strcmp(username,"admin") && !strcmp(password,"123456"))
 {
 printf("登录成功!\n");
 }
 else
 {
 printf("账户或者密码错误!\n");
 }
 
}
int main(int argc,char *argv[])
{
    test7();
    return 0;
}

字符串长度

strlen(字符串)

引入: #include <string.h>

注意: 返回字符串中包含的字符的实际个数,不含 \0

#include <stdio.h>
#include <string.h>
/**
 * 字符串的长度
 * 语法:strlen(字符串|字符数组);
 */
int test8()
{
    // 创建一个char数组,用来存放账户和密码
 char username[20];
 printf("请输入您的账户:\n");
 scanf("%s",username);
// 获取字符串的长度
 unsigned long len = strlen(username);
 
 if(!strcmp(username,"admin"))
 {
 printf("账户输入正确!%lu\n",len);
 }
 else
 {
 printf("账户输入错误!%lu\n",len);
 }
 
}
int main(int argc,char *argv[])
{
    test8();
    return 0;
}

因篇幅问题不能全部显示,请点此查看更多更全内容

热门图文

Copyright © 2019-2025 huatuo0.com 版权所有 湘ICP备2023021991号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务