2016-11-09 19 views
-2

我目前正在學習編程,我不知道爲什麼我的代碼沒有進入那是在做的內部。C:沒有輸入「for」聲明內部做...而

任何想法或幫助將非常有幫助!

#include <stdio.h> 
#include <stdlib.h> 
#include <stdbool.h> 
#include <string.h> 

void swap(char *num1, char *num2) { 
    char temp; 
    temp = *num1; 
    *num1 = *num2; 
    *num2 = temp; 
} 

int main() 
{ 
    int l,i; 
    char s[100]; 
    bool swapped; 

    scanf("%s", s); 
    l=strlen(s); 

    do 
    { 
     swapped = 0; 
     for(i=1;i==(l-1);i++) 
     { 
      if(s[i-1] > s[i]) 
      { 
       swap(&s[i-1],&s[i]); 
       printf("%s\n",s); 
       swapped = 1; 
      }  
     } 
    }while(swapped); 

    // printf("%s\n",s); 

    return 0; 
} 
+2

你開始'I = 1',所以'我==(L-1)'爲真,當且僅如果'l == 2'。也許你的意思是'我<=(l-1)'。 – user3386109

+0

'i ==(l-1)'會檢查'i'是否是'_equat_到'l-1',否則不會進入循環。那是你需要的嗎? – ForceBru

+0

歡迎來到堆棧溢出。您似乎需要學習如何使用調試器逐行執行代碼,這可能使您可以輕鬆查明所遇問題的性質和位置。對於所有的意圖和目的,使用調試器都是任何程序員都需要的知識。有關更多信息,請參閱[如何調試小程序](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。 –

回答

1

您在for循環內的比較檢查是否等於字符串中的字符。如果這種情況是真的,你只會輸入for循環。

您可能想要輸入for循環,然後繼續遞增,我檢查它是否小於或等於字符串中的字符。

示例代碼

char * str = "Hello" 
for(int i = 0; i <= strlen(str) - 1; i++) 
{ 
    printf("%c", str[i]); 
} 

預期輸出

Hello