如何向后打印char数组。 [英] How to print an array of char backward.

查看:123
本文介绍了如何向后打印char数组。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这里有这个代码,它将十进制数转换为二进制数。有一个问题是

。输出打印''反向''我完全没有线索

如何解决这个问题。


程序输出:


十进制数:10

二进制表示:0101

(应该是1010,而不是0101 ......)


如果有人可以帮助我,我会很高兴的。也许有一些功能,

可以解决问题...(我不知道。)


谢谢。


/ ********************************************* ***** ******************** /

#include< stdio.h>

#include< string.h>


int main()

{

char bin [25] ="" ;;

int dec;


printf("十进制数:");

scanf("%d" ;,& dec);


do {

if(dec%2 == 0)

strcat(bin, 0);

else {

strcat(bin," 1");

dec--;

}

dec = dec / 2;

} while(dec 0);

printf("二进制表示:% s \ n",bin);


返回0;

}

/ ******** ****************************************** ******** ************ /

解决方案

hank写道:


我这里有这个代码,它将十进制数转换为二进制数。有一个问题是

。输出打印''反向''我不知道

所有如何解决这个问题。


程序输出:


十进制数:10

二进制表示:0101

(应该是1010,而不是0101 ......)


如果有人可以帮助我,我会很高兴的。也许有一些

函数,这就是诀窍......(我不知道。)



你可以反转字符串在打印之前放置。没有

标准字符串反转功能,但编写一个很容易。作为

的另一种选择,你可以在逐个字符串地逐字逐句地重复打印它。


< snip> ;


您可以在打印之前将字符串反转。没有


标准字符串反向函数,但写一个很容易。作为

的另一种选择,你可以在逐个字符串地逐字逐句地重复打印它。


< snip> ;



这就是我应该做的事情。


(在do-while {}循环之后放这个<) br />

/////////////////////////////////////// /////////////////////////////////

length = strlen(bin);

int i;


for(i = length - 1; i> = 0; i--)

{

printf("%c",bin [i]);

}

///////////// ////////////////////////////////////////////////// ////////


santosh写道:


hank写道:
< blockquote class =post_quotes>
>我这里有这个代码,它将十进制数转换为二进制数。有一个问题。输出打印反向,我不知道如何解决这个问题。



您可以在打印前将字符串反转。



或者你可以写得正确。


/ ************** ************************************ ************** ****** /

#include< stdio.h>

#include< string.h>


void fprint_binary_representation(FILE * out,unsigned char byte){

char str_rep [9] = {0};

int i;

for(i = 7; i> = 0; i--){

str_rep [7-i] =((byte&(1<< i))?''1 '':''0'');

}

fprintf(out,"%s",str_rep);

}


int main(){

fprint_binary_representation(stdout,10); //打印00001010


返回0;

}


/ ********* ***************************************** ********* *********** /


I have this code here, it converts decimal numbers to binary. There is
one problem. The output is printed ''in reverse'' and I have no clue at all
how to solve this problem.

Output of program:

Decimal number: 10
Binary representation: 0101
(It should be 1010, and not 0101...)

I would be glad if someone could help me. Maybe there is some function,
which does the trick... (I have no idea.)

Thanks.

/************************************************** ********************/
#include <stdio.h>
#include <string.h>

int main()
{
char bin[25] = "";
int dec;

printf("Decimal number: ");
scanf("%d", &dec);

do {
if (dec % 2 == 0)
strcat(bin, "0");
else {
strcat(bin, "1");
dec--;
}
dec = dec / 2;
} while (dec 0);
printf("Binary representation: %s\n", bin);

return 0;
}
/************************************************** ********************/

解决方案

hank wrote:

I have this code here, it converts decimal numbers to binary. There is
one problem. The output is printed ''in reverse'' and I have no clue at
all how to solve this problem.

Output of program:

Decimal number: 10
Binary representation: 0101
(It should be 1010, and not 0101...)

I would be glad if someone could help me. Maybe there is some
function, which does the trick... (I have no idea.)

You could reverse the string in place before printing it. There is no
standard string reverse function, but it''s easy enough to write one. As
an alternative you could just iterate backwards through the string
while printing it, character by character.

<snip>


You could reverse the string in place before printing it. There is no

standard string reverse function, but it''s easy enough to write one. As
an alternative you could just iterate backwards through the string
while printing it, character by character.

<snip>

Here''s what I should do.

(Put this after the do-while{} loop)

////////////////////////////////////////////////////////////////////////
length = strlen(bin);
int i;

for (i = length - 1; i >= 0; i--)
{
printf("%c", bin[i]);
}
///////////////////////////////////////////////////////////////////////


santosh wrote:

hank wrote:

>I have this code here, it converts decimal numbers to binary. There is
one problem. The output is printed ''in reverse'' and I have no clue at
all how to solve this problem.


You could reverse the string in place before printing it.

Or you could write it correctly.

/************************************************** ********************/
#include <stdio.h>
#include <string.h>

void fprint_binary_representation(FILE *out, unsigned char byte) {
char str_rep[9] = {0};
int i;
for (i = 7; i >= 0; i--) {
str_rep[7 - i] = ((byte & (1 << i)) ? ''1'' : ''0'');
}
fprintf(out, "%s", str_rep);
}

int main() {
fprint_binary_representation(stdout, 10); // prints 00001010

return 0;
}

/************************************************** ********************/


这篇关于如何向后打印char数组。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆