2016-11-09 20 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