加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

字符串加1问题

(2012-06-08 00:07:19)
标签:

字符串

杂谈

字符串加1问题各位达人,小弟问下用C语言如何实现将字符串加1,例如:a = "010000023459";加1后结果输出字符串为"010000023460"
谢谢了楼主挺幸运的,下面是我写过的一个处理票据号码+1的程序

int bill_chuli(char *bill_no)
{
int bill_no_len = strlen(bill_no);
int zero_no = 0; //票据中包含0的个数
int i = 0;
int bill_int_no; //票据整数字
char bill_no_new = {0}; //数字票据
int j = 0;

if (bill_no_len == 0)
return 0;

if (bill_no == '0')
{
while ((bill_no == '0'))
{
++zero_no;
++i;
if (bill_no == '\0')
return 0;
}

for (; i < bill_no_len; ++i)
{
bill_no_new = bi

一个文件操作的问题

ll_no;
printf("%c\n", bill_no_new);
++j;
}
bill_int_no = atoi(bill_no_new);
++bill_int_no;
memset(bill_no_new, 0, sizeof(bill_no_new));
strncpy(bill_no_new, bill_no, zero_no);
sprintf(bill_no_new + zero_no, "%d", bill_int_no);
memset(bill_no, 0, sizeof(bill_no));
strcpy(bill_no, bill_no_new);

return 0;

}
else
{
bill_int_no = atoi(bill_no);
++bill_int_no;
sprintf(bill_no_new, "%d", bill_int_no);
memset(bill_no, 0, sizeof(bill_no));
strcpy(bill_no, bill_no_new);

return 0;
}


}好人啊!谢谢!比较简单的答案void fun(char *str)
{
int carry=1;
int i=strlen(str);
while(carry&&i)
{
i--;
carry=0;
switch( str[ i ] )
{
case '9':
str[ i ]='0';
carry=1;
break;
case '8':
str[ i ]='9';
break;
case '7':
str[ i ]='8';
break;
case '6':
str[ i ]='7';
break;
case '5':
str[ i ]='6';
break;
case '4':
str[ i ]='5';
break;
case '3':
str[ i ]='4';
break;
case '2':
str[ i ]='3';
break;
case '1':
str[ i ]='2';
break;
case '0':
str[ i]='1';
break;
default:
exit(-1);
}//end of switch
} //end of while

if( (carry==1) && (i==0))
{
exit(-1);//overflow
}
}

[ 本帖最后由 kalashnikova_ak74 于 2009-7-29 02:16 编辑 ]不知道为何 "str" 不能显示........呵呵,短一点http://www.szthcdc.com的版本http://www.2123333.com/:
#include <stdio.h>
#include <string.h>

char *fun(char *s)
{
&nbsp;&nbsp;&nbsp;&nbsp;int i = strlen(s);

&nbsp;&nbsp;&nbsp;&nbsp;while (i-- && s[i] == '9' ? s[i] = '0' : (s[i] += 1, 0));

&nbsp;&nbsp;&nbsp;&nbsp;return s;
}
&nbsp;&nbsp;&nbsp;&nbsp;

int main()
{
&nbsp;&nbsp;&nbsp;&nbsp;char s[] = "00000000000000000000000000";

&nbsp;&nbsp;&nbsp;&nbsp;while (1)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts(fun(s));

&nbsp;&nbsp;&nbsp;&nbsp;return 0;
}



也可以这样写,一样的道理,只是把那个 while 展开了:
#include <stdio.h>
#include <string.h>

char *fun(char *s)
{
&nbsp;&nbsp;&nbsp;&nbsp;int i = strlen(s);

&nbsp;&nbsp;&nbsp;&nbsp;while (i--)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (s[i] == '9')
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s[i] = '0';
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s[i] += 1;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;return s;
}

int main()
{
&nbsp;&nbsp;&nbsp;&nbsp;char s[] = "00000000000000000000000000";

&nbsp;&nbsp;&nbsp;&nbsp;while (1)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts(fun(s));

&nbsp;&nbsp;&nbsp;&nbsp;return 0;
}




代码格式乱了,害我编辑一编~~

[ 本帖最后由 windaoo 于 2009-7-29 02:20 编辑 ]回复 #5 kalashnikova_ak 的帖子kalashnikova_ak:要加上代码框才能显示正确牛人一大把啊。
:luya:先转换成整数数字,+1。原帖由 思一克 于 2009-7-29 10:23 发表 http://bbs3.chinaunix.net/images/common/back.gif
先转换成整数数字,+1。
如果字符串超多就不能了,超过表示的最大数字了

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有