2014-10-23 3 views
-1

12 자로 입력 된 문자를 반복적으로 읽는 프로그램을 실행하려고합니다. 사용자가 입력 한 센티널 값을 입력하면 루프가 종료됩니다. 그러나 첫 번째 문자가 루프에서 읽히 자마자 종료됩니다.루프가 한 번 실행되는 동안

또한 동일한 단어를 다른 배열에 역순으로 배치 한 다음 첫 번째 배열 (앞으로 읽기)이 다른 배열 (뒤로 읽음)과 동일한 지 확인합니다. 그럴 경우 단어가 회문이라는 것을 표시합니다.

#include <stdio.h> 
#include <stdlib.h> 
#include <conio.h> 

int main() 
{ 
int charCount, counter, i, temp, check,check2; 
char letter[12], letter2[12]; 
charCount = 0; 
counter = 10; 
check = 0; 
i = 1; 
check2 = 0; 

printf("Enter your sentinel value.:"); 
scanf_s(" %c", &letter[check2]); 


while ((i<13) && (letter[i] != letter[check2])) 
{ 
    printf("Enter individual letters in word (in order).:"); 
    scanf_s(" %c", &letter[i]); 

    charCount++; 

    if (letter[i] == letter[check2]) 
    { 
     break; 
    } 
    i++; 

} 


printf("Letters entered:%i\n", charCount); 


for (i = 0; i < charCount; i++) 
{ 
    letter2[i] = letter[i]; 
} 

for (i = 0; i <= (charCount/2); i++) 
{ 
    temp = letter2[counter]; 
    letter2[counter] = letter2[i]; 
    letter2[i] = temp; 
    counter--; 
} 

for (i = 0; i <= charCount; i++) 
{ 
    if (letter[i] = letter2[i]) 
    { 
     check++; 
    } 
} 

if (check = charCount) 
{ 
    printf("Word is a palindrome.\n"); 
} 

system("PAUSE"); 
return 0; 

}

+0

1)'scanf_s ("% c", & letter [check2]);'->'scanf_s – BLUEPIXY

+2

@MooingDuck 참조 http://msdn.microsoft.com/en-us/library/w40768et.aspx – BLUEPIXY

+1

2)'(i <13)' ->'(i <12) ("% c", & letter [check2], 1)) ' – BLUEPIXY

답변

0

문자 [1] while 루프 바로 처음 진입 할 때 값이 할당 될 것인가? while 루프 내부의 if 문에서 고려 중이므로 while 루프에서이 조건을 벗어날 수 있다고 생각합니다.

관련 문제