![](/img/trans.png)
[英]How to reverse an array and store the values of the reversed array in a new array
[英]How to reverse a array of chars and then store the reversed array of char to another array?
我一直在研究這個非常簡單的問題。 我試圖反轉字符數組,然后使用C語言將此字符反轉數組存儲到另一個數組中。 這是我的代碼,我無法弄清楚我的代碼有什么問題。 我真的不明白為什么當我嘗試打印出我的stcp時,它是帶有反向字符串的數組,但屏幕上卻沒有任何顯示。 請指教,任何幫助將不勝感激。
#include<stdio.h>
int main() {
char st[100];
scanf("%s", st);
int count = 0;
while(st[count] != '\0'){
count++;
}
//printf("%s", st);
char stcp[100];
int i, j = 0;
for(i = count-1; i >= 0; i--){
st[i] = stcp[j];
j++;
}
puts(stcp);
return 0;
}
我想你是說以下
char stcp[100];
int i = count, j = 0;
while ( i != 0 ) stcp[j++] = st[--i];
stcp[j] = '\0';
至於原始代碼,則必須在此語句中交換操作數
st[i] = stcp[j];
並且字符串stcp
必須附加終止符零。
考慮到函數main應該像C那樣定義
int main( void )
對於我來說,我將變量count
, i
和j
為size_t
類型。
您需要切換此行:
st[i] = stcp[j];
對此:
stcp[j] = st[i];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.