【发布时间】:2015-06-14 15:44:47
【问题描述】:
这是我的程序,它要求用户输入一行文本,然后显示与之关联的 ASCII 码,然后通过在 i 上加 1 来修改 ASCII 码,它还打印出新字符与新的 ASCII 码相关联。
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main()
{
char s[ 70 ]; // define character array of size 70
size_t i; // loop counter
// use gets to get text from user
puts( "Enter a line of text: \a\n" );
fgets( s, 70, stdin );
// convert each character to ASCII and output
puts("\n ASCII code for the characters \n");
for ( i = 0; s[ i ] != '\0'; ++i )
{
printf(" %c:%d",(s[ i ]),(s[ i ]));
} // end for Uppercase ASCII
// convert each character to modified ASCII and output
puts( "\nThe line modified with progressively higher ASCII:\n" );
for ( i = 0; s[ i ] != '\0'; ++i )
{
printf( "%c", ( s[ i ] + (i+1)) );
} // end for modified characters
puts("");
printf( " \n modified ASCII code for the entered string \n\n\a");
for ( i = 0; s[ i ] != '\0'; ++i )
{
printf( " %c:%d",(s[ i ] + (i+1)),(s[ i ] +(i+1)));
} // end for modified ASCII code
printf( "\n \t The final value of loop variable 'i' is:%d\n", i);
puts( "" );
system("PAUSE");
}
我很难修改它,以便 ASCII 代码和字符由前七个素数 2,3,5...17 改变,而在前 7 个素数上升后,反方向 17,13, 11,7,...2。所以在第一个循环之后说 g 的 ASCII 码是 103,修改后的 ASCII 码现在是 105,新字母现在是 i。谁能帮帮我?
【问题讨论】:
-
对不起,我刚刚编辑了它,这是我的错误
-
提示:你怎么知道
s[ i ] + (i+1)是可打印的ASCII? -
%d 以 ASCII 格式打印出字母,您只需输入一串字符,然后将其从字符 %c 转换为 ASCII %d。
-
s[ i ] + (i+1) 是修改 ASCII 的程序部分,i 在每次循环后都会改变,它会变大 ++i 所以对于第一个循环它是 0下一个循环 1 等。
标签: c