H面试程序(27):字串转换
//1 字串转换
//问题描述:
//将输入的字符串(字符串仅包含小写字母‘a’到‘z’),按照如下规则,循环转换后输出:a->b,b->c,…,y->z,z->a;
//若输入的字符串连续出现两个字母相同时,后一个字母需要连续转换2次。
//例如:aa 转换为 bc,zz 转换为 ab;当连续相同字母超过两个时,第三个出现的字母按第一次出现算。
//要求实现函数:
//void convert(char *input,char* output)
//【输入】 char *input , 输入的字符串
//【输出】 char *output ,输出的字符串
//【返回】 无
//40min
//示例
//输入:char*input="abcd"
//输出:char*output="bcde"
//输入:char*input="abbbcd"
//输出:char*output="bcdcde"
#include<stdio.h>
#include<assert.h>
void convert(char *input,char* output)
{
assert(input);
assert(output);
while(*input != '\0') //遍历整个字符数组
{
if(*input == *(input+1)) //当前字符和下一字符相同的情况
{ if(*input =='z') //因为字母‘z’的情况比较特殊,所以单独分开
{
*output = (*input-25);
*(output+1) =(*input - 24);
output = output + 2;
input = input + 2;
}
else //
{
*output = (*input + 1);
*(output+1) = (*input + 2);
output = output + 2;
input = input + 2;
} }
else //当前字符和下一字符不相同的情况
{
if(*input =='z')//因为字母‘z’的情况比较特殊,所以单独分开
{
*output++ = (*input-25); input++; }
else
{
*output++ = (*input + 1);
input++;
}
} }
*output = '\0'; //记得在output后加上结束符号
} int main( )
{
char input[] = "abbbcd";
printf("%s\n",input);
char output[100];
convert(input, output);
printf("%s\n",output);
return 0;
}
H面试程序(27):字串转换的更多相关文章
- H面试程序(10): 字符串包含问题
题目描述:判断第二个字符串中的元素是否都能在第一个字符串中找到: 注意:和字符串的字串的问题有所区别,如第一个字符串为 abcdefg,第二个字符串为 aaabc,第二个字串还是包含于第一个字符串 ...
- H面试程序(11): 判断字符串是否包含子串问题
题目描述: 如字符串str1为''abcdef''' 字符串str2为'' bc''; 则字符串str1中含有 ...
- H面试程序(28):字符串处理转换
//2 字符串处理转换 //问题描述: //在给定字符串中找出单词( “单词”由大写字母和小写字母字符构成, //其他非字母字符视为单词的间隔,如空格.问号.数字等等:另外单个字母不算单词): //找 ...
- H面试程序(12): 输出字符串中第一个只出现一次的字母
题目描述: 若字符串str为'' sbdddsbfc'',则输出 f; 若字符串str为''aabbccdd'',则输出:字符串str中的字符都出现两次以上 #include <stdio.h& ...
- H面试程序(29):求最大递增数
要求:求最大递增数 如:1231123451 输出12345 #include<stdio.h> #include<assert.h> void find(char *s) { ...
- H面试程序(15): 冒泡排序法
#include<stdio.h> #include<assert.h> void display(int * a, int n) { for(int i = 0; i < ...
- H面试程序(16): 简单选择排序
#include<stdio.h> #include<assert.h> void display(int * a, int n) { assert(a); for(int i ...
- H面试程序(4):翻转句子中单词的顺序 .
题目:输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变. 句子中单词以空格符隔开.为简单起见,标点符号和普通字母一样处理. 例如输入“I am a student.”,则输出“stude ...
- H面试程序(1)编写一个函数,要求输入年月日时分秒,输出该年月日时分秒的 下一秒
编写一个函数,要求输入年月日时分秒,输出该年月日时分秒的下一秒. 如输入 2004 年 12 月 31 日 23 时 59 分 59 秒,则输出 2005年 1 月 1 日 0 时 0 分 0 秒. ...
随机推荐
- BZOJ 1974: [Sdoi2010]auction 代码拍卖会( dp )
在1, 11, 111……中选<=8个, + 11..(n个1)拼出所有可能...这些数mod p至多有p中可能, 找出循环的处理一下. 那么dp就很显然了...dp(i, j, k)表示前i种 ...
- 学习PS必须弄懂的专业术语
在学习PS的过程中,我们经常会遇到一些专业术语,下面我们来对一些常用的.比较难理解的术语进行简单讲解. 像素:像素是构成图像的最基本元素,它实际上是一个个独立的小方格,每个像素都能记录它所在的位置和颜 ...
- hdu 4034 Graph floyd
题目链接 给出一个有向图各个点之间的最短距离, 求出这个有向图最少有几条边, 如果无法构成图, 输出impossible. folyd跑一遍, 如果dp[i][j] == dp[i][k]+dp[k] ...
- [LeetCode]题解(python):138-Copy List with Random Pointer
这道题目不是太懂,参考了http://www.cnblogs.com/zuoyuan/p/3745126.html的博客. 题意: A linked list is given such that e ...
- 在输出视图中使用使用html注释会导致在Chrome中css失效
在做SportsStore例子时,在视图List.cshtml中使用了html注释,即 <!-- 注释 --> 结果在加载css时,chrome浏览器中所有css效果都失效.IE不受影响. ...
- C# async await 例子
private static async void Worker() { Console.Write("main thread id is :{0}",Thread.Current ...
- png图片压缩优化
1.2 软件环境 软件名称:Opting下载地址: http://optipng.sourceforge.net/ 安装版本:0.7.5安装位置:/apps/svr/opting 安装可能遇到的问题: ...
- Gartner 认定 Microsoft 为具有远见卓识的云基础结构即服务提供商
四个月前, Windows Azure 基础结构服务结束了预览版阶段,正式发布了,它具有业内领先的 SLA.随后, 凭借愿景的完整性和执行力,Gartner 很快认可了 Microsoft 在市场中的 ...
- 最长回文子串 | 勇幸|Thinking
最长回文子串 | 勇幸|Thinking 最长回文子串
- hdu 4741 Save Labman No.004 [2013年杭州ACM网络赛]
// Time 234 ms; Memory 244 K #include<iostream> #include<cstdio> #include<cmath> u ...