嵌套开关导致无限循环

Nested switch leads to an infinite loop

在下面的程序中,我需要将初始字符和最终字符更改为它们各自的字符,如下所述以防万一,但这给了我一个无限循环。我应该怎么做才能解决它?

int main(void)
{
 char state ='t';
 char word[20]="aaabbccaaaaccbbb";

 int initiallength = strlen(word)-1; strcat(word,"a");
 while(strlen(word)-1 >initiallength)
 {
   switch(state)
   {
     case 't':
       switch(word[strlen(word)-1])
       {
         case 'a':
           word[strlen(word)-1]='b'; break;
         case 'b':
           word[strlen(word)-1]='c'; break;
         case 'c':
           word[strlen(word)-1]='d'; break;
         case 'd':
           word[strlen(word)-1]='[=10=]'; break;
       }
       switch(word[0])
       {
         case 'a':
           word[0]='b'; break;
         case 'b':
           word[0]='c'; break;
         case 'c':
           word[0]='d'; break;
         case 'd': 
           word[0]='[=10=]'; break;
       }
   }
 }
}

如果我理解正确的话,你想要做的是交换给定字符串中的第一个和最后一个字符。如果是这种情况,首先你的代码太复杂了,其次你得到无限循环的原因是因为条件 strlen(word)-1 >initiallength 总是为真。

测试单词是否为空

int main(void){                                                                
    char state ='t';                                                           
    char word[20]="aaabbccaaaaccbbb";                                          

    int initiallength = strlen(word)-1;                                        

    strcat(word,"a");                                                          

    while(strlen(word)-1 >initiallength && strlen(word) >= 0){                 
        printf("%d %d\n", strlen(word)-1, initiallength);                      
        printf("%d len %s\n", strlen(word), word);                             
        switch(state){                                                         
          case 't':                                                            
            switch(word[strlen(word)-1]){                                      
              case 'a':                                                        
              case 'b':                                                        
              case 'c':                                                        
                word[strlen(word)-1]++;                                        
                break;                                                         
              case 'd':                                                        
                word[strlen(word)-1] = '[=10=]';                                   
                break;                                                         
            }                                                                  

            switch(word[0]){                                                   
              case 'a':                                                        
              case 'b':                                                        
              case 'c':                                                        
                word[0]++;                                                     
                break;                                                         
              case 'd':                                                        
                word[0] = '[=10=]';                                                
                break;                                                         
            }                                                                  
        }                                                                      
    }                                                                          
}