Error[8]: Undefined offset: 40, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

字符串

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

字符串变量
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
using namespace std;

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

	while (1)
	{}
	return 0;
}

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

字符串输入输出 字符串输入输出
#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 &&& %s\n", str, str2);

	while (1)
	{}
	return 0;
}

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

 

 安全输入 常见错误 空字符串 字符串数组 char **a

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

#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;
}
程序参数

#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:%s\n",i,argv[i]);
	}
	while (1)
	{}
	return 0;
}

输出:

单字符输入输出  getchar putchar
#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("EOF\n");
	while (1)
	{}
	return 0;
}

 输出:

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

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

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

  • size_t strlen(const char*s);
  • 键之后,计算机刷新缓冲区,将这个缓冲区的内容打印出来。

     字符串函数strlen

    strlen  strcmp  strcpy  strcat  strchr  strstr

    #include

    strlen
    #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: %lu\n", strlen(word));
    	printf("word mylen: %lu\n", mylen(word));
    	printf("word sizeof: %lu\n", sizeof(word));
    	while (1)
    	{}
    	return 0;
    }
    
    int mylen(const char* c_str)
    {
    	int cnt = 0;
    	while (*c_str++ != '\0')
    	{
    		cnt++;
    	}
    	return cnt;
    }

     

     字符串函数strcmp strcmp
    1. -1:s1  < s2
    2. 1:s1  > s2
    3. char *strcpy(char*dst,char*src)
    #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: %d\n",strcmp(s1,s2));
    	printf("mycmp: %d\n", mycmp(s1, s2));
    	while (1)
    	{}
    	return 0;
    }
    
    int mycmp(const char* s1, const char* s2)
    {
    	int ret = 0;
    	while (*s1 ==*s2 && *s1 != '\0')
    	{
    		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 复制一个字符串           
    #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 %s\n", cpy1);
    	printf("mycpy %s\n", cpy3);
    	while (1)
    	{}
    	return 0;
    }
    
    char *mycpy(char *dst, const char*src)
    {
    	char *ret = dst;
    	while (*dst++ = *src++)
    	{
    		;
    	}
    	*dst = '\0';
    	return ret;
    }

       输出:

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

     

    )
    File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
    File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
    File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
    字符串基础知识_C_内存溢出

    字符串基础知识

    字符串基础知识,第1张

    字符串
    • #include
    • 以0(整数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 :%d\n", sizeof(work));
    	printf("work strlen :%d\n", strlen(work));
    	char work_arr[] = "working";
    	printf("work_arr sizeof :%d\n", sizeof(work_arr));
    	printf("work_arr 1strlen :%d\n", strlen(work_arr));
    	char *work_str = "working";
    	printf("work_str sizeof :%d\n", sizeof(work_str));//表示指针字节长度
    	printf("work_str 1strlen :%d\n", 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 &&& %s\n", 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
    • '
    • int main(int argc,const char *argv[ ])
    字符串数组 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;
    }
    
    程序参数
    • argv[0]是命令本身
    • 使用Unix的符号链接本身,反应符号链接的名字(表示输入的参数)
    • int getchar(void)

    #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:%s\n",i,argv[i]);
    	}
    	while (1)
    	{}
    	return 0;
    }

    输出:

    单字符输入输出  getchar
    • 从标准输入读入一个字符
    • 返回类型是int为了返回EOF(-1)
    • int putchar(int c)
    • Windows ->Ctrl-Z
    • Unix ->Ctrl-D
    putchar
    • 向标准输出写一个字符
    • 返回写了几个字符,EOF(end of file)(-1)表示写失败
    • Enter
    #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("EOF\n");
    	while (1)
    	{}
    	return 0;
    }

     输出:

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

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

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

  • size_t strlen(const char*s);
  • 键之后,计算机刷新缓冲区,将这个缓冲区的内容打印出来。

     字符串函数strlen

    strlen  strcmp  strcpy  strcat  strchr  strstr

    #include

    strlen
    • 返回s的字符长度(不包括结尾的0)
    • int strcmp(const char *s1,const char*s2)
    #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: %lu\n", strlen(word));
    	printf("word mylen: %lu\n", mylen(word));
    	printf("word sizeof: %lu\n", sizeof(word));
    	while (1)
    	{}
    	return 0;
    }
    
    int mylen(const char* c_str)
    {
    	int cnt = 0;
    	while (*c_str++ != '\0')
    	{
    		cnt++;
    	}
    	return cnt;
    }

     

     字符串函数strcmp strcmp
    • 比较两个字符串,返回值:
    • 0:s1 == s2
    1. -1:s1  < s2
    2. 1:s1  > s2
    3. char *strcpy(char*dst,char*src)
    #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: %d\n",strcmp(s1,s2));
    	printf("mycmp: %d\n", mycmp(s1, s2));
    	while (1)
    	{}
    	return 0;
    }
    
    int mycmp(const char* s1, const char* s2)
    {
    	int ret = 0;
    	while (*s1 ==*s2 && *s1 != '\0')
    	{
    		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
    • 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 %s\n", cpy1);
    	printf("mycpy %s\n", cpy3);
    	while (1)
    	{}
    	return 0;
    }
    
    char *mycpy(char *dst, const char*src)
    {
    	char *ret = dst;
    	while (*dst++ = *src++)
    	{
    		;
    	}
    	*dst = '\0';
    	return ret;
    }

       输出:

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

     

    欢迎分享,转载请注明来源:内存溢出

    原文地址: https://outofmemory.cn/langs/1325863.html

    (0)
    打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
    上一篇 2022-06-12
    下一篇 2022-06-12

    发表评论

    登录后才能评论

    评论列表(0条)

    保存