面试题目(2)——转换字符串-插入字符个数
2008-02-29 14:28
295 查看
题目要求:
转换字符串——插入字符的个数,如字符串aaab,插入字符个数后变为aaa3b1。代码如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//************************************************************************
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 转换字符串——插入字符的个数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 如,aaab,变为aaa3b1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
************************************************************************/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <stdlib.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <stdio.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <string.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#define MAXCOUNT 2*100 //输入的字符串长度不能超过100
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
char *transformation(char *str)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int len=strlen(str);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
char *buf=new char[len+1]; //存放某个字符的个数即该字符后的字符串
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
char *p=str;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
char *q=p+1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int count=1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
while(*q)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(*p==*q)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
count++;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p++;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
q++;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
itoa(count,buf,10);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int nbits=strlen(buf);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
strcat(buf,q);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*q=0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
strcat(str,buf);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
q+=nbits;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p=q;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
q=p+1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
count=1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//最后一个相同字符的个数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
itoa(count,buf,10);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
strcat(str,buf);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
delete []buf;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
buf=NULL;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return str;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//显示菜单
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void show_menu()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("--------------------------------------------- ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("input command to test the program ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf(" i or I : input string to test ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf(" q or Q : quit ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("--------------------------------------------- ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("$ input command >");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void main()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
char sinput[10];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
char str[MAXCOUNT];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
show_menu();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
scanf("%s",sinput);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
while(stricmp(sinput,"q")!=0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(stricmp(sinput,"i")==0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf(" please input an string:");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
scanf("%s",&str);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf(" before transformation: %s ",str);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
char *pstr=transformation(str);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf(" after transformation: %s ",pstr);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//输入命令
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("$ input command >");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
scanf("%s",sinput);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
运行结果如下:
相关文章推荐
- 字符串的操作:取字符, 替换字符,字符拼接,转换,插入字符等
- 编程实现转换字符串、插入字符的个数
- 经典算法面试题目-判断一个字符串中的字符是否唯一(1.1)
- 面试宝典:转换字符串格式为原来字符串里的字符+该字符连续出现的个数
- [置顶] Android面试题目之二:整形转换为字符串
- 经典算法面试题目-判断一个字符串中的字符是否唯一(1.1)
- C语言学习序列之面试题目-计算两个字符串中出现的相同英文字符
- 【剑指Offer面试编程题】题目1508:把字符串转换成整数--九度OJ
- 经典算法面试题目-设计算法移除字符串中重复的字符(1.3)
- 微软面试100题目之17 :在一个字符串中找到第一个只出现一次的字符。
- 【剑指Offer面试编程题】题目1508:把字符串转换成整数--九度OJ
- 经典算法面试题目-设计算法移除字符串中重复的字符(1.3)
- 题目1049:字符串去特定字符
- JNI中 Java 中字符串 转换成C中的字符数组方法
- C 这个字符串参数必须包含一个或者多个数字,函数应该把这些数字字符转换为整数并返回这个整数。如果字符串参数包含了任何非数字字符,函数就返回零。
- asp.net(c#)字符串转换成字符数组 字符串转换成int 数组
- C/C++面试之算法系列--atoi(char *str)将字符串转换成整数
- 经典算法面试题目-判断s2是否是s1的旋转字符串(1.8)
- QT中将字符串转换为每两个字符添加一个空格的格式
- 练习 3-2 编写一个函数escape(s, t),将字符串t 复制到字符串s 中,并在复制过程中将换行符、制表符等不可见字符分别转换为\n、\t等相应的可见的转义字符序列。要求使用swich语句。