我怎样才能在z之后再次打印abcd? [英] How can I print after z again abcd like that?
本文介绍了我怎样才能在z之后再次打印abcd?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
接受字符C和正整数N作为输入。该程序必须从C开始打印N个字符。
边界条件:
1< = N< = 100
输入格式:
第一行包含由空格分隔的C和N.
输出格式:
第一行包含N个字符。
示例输入/输出1:
输入:
a 4
输出:
abcd
示例输入/输出2:
输入:
z 5
输出:
zabcd
我尝试过:
Accept a character C and a positive integer N as input. The program must print N characters starting from C.
Boundary Condition(s):
1 <= N <= 100
Input Format:
The first line contains C and N separated by space(s).
Output Format:
The first line contains N characters.
Example Input/Output 1:
Input:
a 4
Output:
abcd
Example Input/Output 2:
Input:
z 5
Output:
zabcd
What I have tried:
#include<stdio.h>
#include <stdlib.h>
int main()
{
char a;
int n,b=0;
scanf("%c",&a);
scanf("%d",&n);
for( char i=a;b<n;i++){
printf("%c",i);
b=b+1;
}
}
推荐答案
由于字母表中有26个字母,您可以使用% 26 在z之后再次从头开始。
然而,字符a不是0,所以你需要先减去它然后重新添加它。
例如:
Since there are 26 letters in the alphabet, you can use % 26 to start from the beginning again after z.
The character 'a' is not 0 however, so you need to subtract it first and then later re-add it.
For example:
for (int i = 0; i < n; i++) {
printf("%c", (a - 'a' + i) % 26 + 'a');
}
for (i=0; i<n; ++i)
{
putchar(a);
a = (a == 'z') ? 'a' : (a + 1);
}
在循环os中有2个变量基本上就是这个想法,但你的问题是你正在混合它们的用法。 br />
保持它们的使用分开,有1个变量来计算打印的字母,另一个变量用于跟踪要打印的字母。
Having 2 variables in the loop os basically the idea, but your problem is the you are mixing their usage.
Keep their usage separated, have 1 variable to count printed letters, and the other variable to keep track of the letter to print.
for( int i=0;i<n;i++){ // count here
printf("%c",a);
if (a<'z') // set next letter here
a=a+1;
else
a='a';
}
这篇关于我怎样才能在z之后再次打印abcd?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文