栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

字符串基础知识

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

字符串基础知识

字符串
  • #include
  • 以0(整数0)结尾的一串字符

->0或“”是一样,但和‘0’不同

  • 0字符串结束标志,但它不是字符串的一部分,字符串长度是我们眼睛所看到的字符字面量长度(不包括这个结束标志0)
  • 字符串以数组形式存在,以数组或指针形式访问(可以通过数组的方式遍历字符串)
字符串变量
  • char work[10] = "working";
  • char work_arr[] = "working"; //字符串字面量用来初始化数组  
  • char *work_str = "working";
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
using namespace std;

int main()
{
	printf("字符串---------------n");
	char work[10] = "working";
	printf("work sizeof :%dn", sizeof(work));
	printf("work strlen :%dn", strlen(work));
	char work_arr[] = "working";
	printf("work_arr sizeof :%dn", sizeof(work_arr));
	printf("work_arr 1strlen :%dn", strlen(work_arr));
	char *work_str = "working";
	printf("work_str sizeof :%dn", sizeof(work_str));//表示指针字节长度
	printf("work_str 1strlen :%dn", strlen(work_str));

	while (1)
	{}
	return 0;
}

输出:(strlen计算字符串长度)

字符串输入输出 字符串输入输出
  • char string[8];
  • scanf("%s",string);
  • printf("%s",string);
  • scanf读入一个单词(直到空格/tab/回车为止)
  • scanf不安全,不知道读入内容长度
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
using namespace std;

int main()
{
	printf("字符串输入和输出--------------n");
	char str[20];
	char str2[20];
	scanf("%s", str);
	scanf("%s", str2);
	printf("*** %s &&& %sn", str, str2);

	while (1)
	{}
	return 0;
}

输出:scanf输出遇到空格停止,没有将显示屏输入的字符完全输出,分别给到了str、str2两个变量

 

 安全输入
  • char string[8];
  • scanf("%7s",string);
  • 7:表示最多允许读入字符数,比数组小1,7 = 8 - 1
常见错误
  • char *string; //指针没有指向实际有效的地址 
  • scanf("%s",string);
  • 定义字符串类类型变量string,但并没有初始化为0,所有运行的时候可能会出错
空字符串
  • char buffer[100] =" ";//空字符串,buffer[0] == ''
  • char buffer[ ] =" "; //数组长度只有1
字符串数组 char **a

    a是一个指针,指向另一个指针,这个指针指向一个字符(串)

  • char a[ ][ ] 二维数组变量 第二位长度是个确定值(a是个数组 a[0]->char [10]) 
  • char *a[ ] (a[0]->char*)
#define _CRT_SECURE_NO_WARNINGS
#include
#include
using namespace std;

int main()
{
	printf("字符串数组--------------n");
	char a[][10] = { "Hello","World","abcdefg" }; //a[0] ->char[10]
	char *a[] = { "Hello","World","abcdefg" }; //a[0] ->char *
    char *seasons[4] = {"Winter","Spring","Summer","Fall"};
	while (1)
	{}
	return 0;
}
程序参数
  • int main(int argc,const char *argv[ ])
  • argv[0]是命令本身
  • 使用Unix的符号链接本身,反应符号链接的名字(表示输入的参数)

#define _CRT_SECURE_NO_WARNINGS
#include
#include
using namespace std;

int main(int argc, const char *argv[])
{
	printf("字符串数组--------------n");
	printf("n");
	for (int i = 0; i < argc; i++)
	{
		printf("string-->%d:%sn",i,argv[i]);
	}
	while (1)
	{}
	return 0;
}

输出:

单字符输入输出  getchar
  • int getchar(void)
  • 从标准输入读入一个字符
  • 返回类型是int为了返回EOF(-1)
  • Windows ->Ctrl-Z
  • Unix ->Ctrl-D
putchar
  • int putchar(int c)
  • 向标准输出写一个字符
  • 返回写了几个字符,EOF(end of file)(-1)表示写失败
#define _CRT_SECURE_NO_WARNINGS
#include
#include
using namespace std;

int main(int argc, const char *argv[])
{
	printf("单字符输入输出--------------n");
	int ch = 0;
	while ((ch = getchar()) != EOF)
	{
		putchar(ch);
	}
	printf("EOFn");
	while (1)
	{}
	return 0;
}

 输出:

缓冲输入:用户输入字符后,字符会存入一个缓冲区中,直到用户按下Enter键,才会打印缓冲区的字符。

while循环,输入一个字符串,判断这个字符串是否是EOF字符,如果不是,执行putchar(),用户输入的字符储存在缓冲区,用户没按下

首先先输入一个字符,判断这个字符是否是EOF字符,若不是,则执行putchar(),此时用户输入的字符存储在缓冲区中,但没有按下Enter键,那么程序即使执行到putchar(),也不会将这个字符打印出来,因为压根就没有刷新缓冲区,当用户按下Enter键之后,计算机刷新缓冲区,将这个缓冲区的内容打印出来。

 字符串函数strlen

strlen  strcmp  strcpy  strcat  strchr  strstr

#include

strlen
  • size_t strlen(const char*s);
  • 返回s的字符长度(不包括结尾的0)
#define _CRT_SECURE_NO_WARNINGS
#include
#include
using namespace std;
int mylen(const char* c_str);

int main(int argc, const char *argv[])
{
    printf("字符串函数strlen--------------n");
	char word[] = "Hello good good study,day day up!";
	printf("word strlen: %lun", strlen(word));
	printf("word mylen: %lun", mylen(word));
	printf("word sizeof: %lun", sizeof(word));
	while (1)
	{}
	return 0;
}

int mylen(const char* c_str)
{
	int cnt = 0;
	while (*c_str++ != '')
	{
		cnt++;
	}
	return cnt;
}

 

 字符串函数strcmp strcmp
  • int strcmp(const char *s1,const char*s2)
  • 比较两个字符串,返回值:
  1. 0:s1 == s2
  2. 1:s1  > s2
  3. -1:s1  < s2
#define _CRT_SECURE_NO_WARNINGS
#include
#include
using namespace std;
int mycmp(const char* s1, const char* s2);

int main(int argc, const char *argv[])
{
    printf("字符串函数strcmp--------------n");
	char s1[] = "Abc";
	char s2[] = "ab";
	printf("strcmp: %dn",strcmp(s1,s2));
	printf("mycmp: %dn", mycmp(s1, s2));
	while (1)
	{}
	return 0;
}

int mycmp(const char* s1, const char* s2)
{
	int ret = 0;
	while (*s1 ==*s2 && *s1 != '')
	{
		s1++;
		s2++;
	}
	if (*s1 - *s2 > 0)
	{
		ret = 1;
	}
	else if (*s1 - *s2 < 0)
	{
		ret = -1; 
	}
	
	return ret;
}

//先把strcmp的源码贴出来先
int __cdecl strcmp (
        const char * src,
        const char * dst
        )
{
        int ret = 0 ;

        while((ret = *(unsigned char *)src - *(unsigned char *)dst) == 0 && *dst)
                ++src, ++dst;

        if ( ret < 0 )
                ret = -1 ;
        else if ( ret > 0 )
                ret = 1 ;

        return( ret );
}

  

 字符串函数strcpy strcpy
  • char *strcpy(char*dst,char*src)
  • src字符串拷贝到dst,返回dst 
复制一个字符串           
  • char *dst = (char*)malloc(strlen(src)+1) ;
  • strcpy(dst,src);
#define _CRT_SECURE_NO_WARNINGS
#include
#include
using namespace std;
char *mycpy(char *dst, const char*src);

int main(int argc, const char *argv[])
{
	printf("n");
	printf("字符串函数strcpy--------------n");
	char cpy1[] = "";
	char cpy2[] = "abcd------";
	char cpy3[] = "";
	char cpy4[] = "good good study,day day up";
	strcpy(cpy1, cpy2);
	mycpy(cpy3, cpy4);
	printf("strcpy %sn", cpy1);
	printf("mycpy %sn", cpy3);
	while (1)
	{}
	return 0;
}

char *mycpy(char *dst, const char*src)
{
	char *ret = dst;
	while (*dst++ = *src++)
	{
		;
	}
	*dst = '';
	return ret;
}

   输出:

 参考:B站->《C语言入门与进阶 翁恺》视频

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/957640.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号