2017-08-10 125 views
0
#include <stdio.h> 
#include <conio.h> 
#include <string.h> 


int main() { 
    int x,y,z,a; 
    char arr[221]; 
    char temp; 

    printf ("Enter values: "); 
    gets(arr); 

    a = strlen(arr); 
    x=a; 
    while (x!=-1){ 
      printf("\n%s", &arr[x]); 
      x--; 
    }  
    getch(); 
} 

輸出必須是這樣的任何人都可以在c中幫助我嗎?

採樣輸入:

A 
2 
1 
R 
X 
D 
W 

注:由於A是第一個條目到堆棧,堆應該是這樣的:

W D X R 1 2 A 

「A」是在所述陣列或電池堆的很遠端,這意味着「A」是第一個從堆棧出去。然後,它後面是2,1,R,等等...

你的輸出應該是這樣的:

A 
W D X R 1 2 

2 
W D X R 1 

1 
W D X R 

R 
W D X 

X 
W D 

D 
W 

W 
+0

使用'得到()'強烈反對。 'conio.h'不是標準的一部分。 –

+0

一些提示可以幫助你ASCII 65至122是字母 –

+0

'X = A-1'代替'X = A'和'的printf( 「\ n%C」,ARR [X]);'代替'的printf( 「\ n%S」,&ARR [X]);' –

回答

0

試試這個:

x=a-1; 
while (x!=-1){ 
     printf("\n%c", &arr[x]); 
     x--; 
} 

使用x=a-1%c代替x=1%s

0

首先你使用的是gets(),所以輸入必須遵循這種格式A21RXDW。那是一個字符串。只有在這種情況下,棧頂纔能有'A'

現在

char input[100]={'\0'}; //best practice 
scanf("%s",input); 
int stack_top = strlen(input) - 1; 
//iterate 
while(stack_top >= 0){ 
    printf("%c\n", input[stack_top]); 
    --stack_top; 
} 
相關問題