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 秒. ...
随机推荐
- js写的简单轮播图
这个轮播图代码是从网上找来的,专门找了个写法简单的,只是作为一个小练习,大概原理如下: 1.首先是图片切换2.自动播放3.调用自动播放4.移动到容器上边停止播放,离开自动播放5.移动到导航上停止播放, ...
- javascript函数apply和call
apply:方法能劫持另外一个对象的方法,继承另外一个对象的属性. Function.apply(obj,args)方法能接收两个参数obj:这个对象将代替Function类里this对象args:这 ...
- System类基础
取时间差: public class SystemDemo01 { public static void main(String[] args) { long startTim ...
- [Laravel 5] 表单验证 Form Requests and Controller Validation
本文 转载自:http://blog.hsin.tw/2015/laravel-5-note09-form-requests-and-controller-validation/ 文章解答了我的困惑非 ...
- 获取多个汉字首字母(php)
<?php function getfirstchar($s0){ $fchar = ord($s0{0}); if($fchar >= ord("A") and $f ...
- poj 2584 T-Shirt Gumbo 网络流
题目链接 有5种T-shirt, n个人, 每个人可以接受某些种T-shirt, 每种T-shirt的数量已知, 问每个人能否都穿上自己能接受的T-shirt. 源点向每种T-shirt连边, 权值为 ...
- JS笔记 入门第二
输出内容 document.write(); alert("hello!"); alert(mynum); </script> 注:alert弹出消息对话框(包含一个确 ...
- C#对象赋值出现的诡异问题,或许你也遇到过,有待你的解决
前言:今天在代码中,又出现了这个问题,就是对象赋值给一个新的对象时,然后更改新对象中的属性,就会把老对象的值也更改,以前也遇到这个问题,只是没有深究,今天刚好又遇到了此问题,我决定写下来,和大家一起分 ...
- Linux2.6中的Slab层
还记得一个进程创建的时候是什么给它分配的“进程描述符”吗?没错,是slab分配器,那么,这个slab分配器是个什么东西呢? 分配和释放数据结构是所有内核中最普遍的操作之一.为了 ...
- windows下搭建apache+php+mysql
在windows下,apache和mysql都有自动化安装的程序,本篇则侧重从apache和php版本选择,php线程安全,apache和mysql安装启动服务,工作环境配置这几个方面来阐述windo ...