ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

字符串中的连续字符统,并将统计数字插入到原有字符串中 -- C语言

2020-02-26 13:05:04  阅读:294  来源: 互联网

标签:stat C语言 statStr 0x7fffffffe450 gdb 498 str 字符串 统计数字


题目

需要在字符串中插入字符统计的个数。 例如字符串 aaabc, 插入字符个数后变成 aaa3b1c1

 

代码实现

int statStr(char * str){
	if(NULL == str){
		printf("statStr param error\n");
		return PARAM_ERR;
	}

	int len = strlen(str) + 1; /*加上 '\0'*/
	int scanlen = 0; /*扫描的长度,从1开始*/
	int stat = 1;
	char * p = NULL;
	char * q = NULL;
	char s;

	p = str;
	while('\0' != *p){
		q = p;
		stat = 0; /*至少有自己重复*/
		while('\0' != q && *q == *p){
			stat++;
			q++;
		}
		scanlen += stat;

		s = stat + '0';
		memcpy(p + stat + 1, p + stat, len - scanlen);	
		*(p + stat) = s; /*填入统计数字*/
		scanlen += 1; 	/*因为插入了一个数字*/
		len = len + 1; 	/*因为插入了一个数字*/
		p = p + stat + 1; /* + 1 因为插入了一个数字*/
	}

	return SUCCESS;	
	
}


void teststatStr(void){
	char str[100] = "aaabc";

	printf("\n************  teststatStr ************ \n");

	statStr(str);

	printf("Stat str is: %s\n", str);

	return;	
}

代码编译

gcc main.c str.c -g -o a.exe

调试输出

GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-115.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/renmian/interview/a.exe...done.
(gdb) b str.c:498
Breakpoint 1 at 0x4015b3: file str.c, line 498.
(gdb) r
Starting program: /home/renmian/interview/./a.exe


************  teststatStr ************

Breakpoint 1, statStr (str=0x7fffffffe450 "aaabc") at str.c:498
498                     q = p;
(gdb) p str
$1 = 0x7fffffffe450 "aaabc"
(gdb) n
499                     stat = 0; /*至少有自己重复*/
(gdb) c
Continuing.

Breakpoint 1, statStr (str=0x7fffffffe450 "aaa3bc") at str.c:498
498                     q = p;
(gdb) p str
$2 = 0x7fffffffe450 "aaa3bc"
(gdb) n
499                     stat = 0; /*至少有自己重复*/
(gdb) c
Continuing.

Breakpoint 1, statStr (str=0x7fffffffe450 "aaa3b1c") at str.c:498
498                     q = p;
(gdb) p str
$3 = 0x7fffffffe450 "aaa3b1c"
(gdb) c
Continuing.
Stat str is: aaa3b1c1
[Inferior 1 (process 14942) exited normally]
(gdb) p str
No symbol "str" in current context.
(gdb) q

 

leoufung 发布了212 篇原创文章 · 获赞 44 · 访问量 26万+ 私信 关注

标签:stat,C语言,statStr,0x7fffffffe450,gdb,498,str,字符串,统计数字
来源: https://blog.csdn.net/leoufung/article/details/104514925

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有