"C Primer Plus"책의 연습 문제를 쓰려고합니다. 그들 중 한 명은 내가 해결할 수 없거나 진행 상황을 파악할 수 없었습니다. (필요에 따라)문자열의 첫 번째 문자는 어디에 있습니까?
#include <stdio.h>
int main()
{
char str[500];
gets(str);
puts(str);
return 0;
}
출력 : 단계별 시련을 디버깅 후, 난 그냥이 테스트
정확한 LY 모든 것은 내가
를 입력내가 입력 정확한 LY 모든 것
하지만 운동하려는 것에 대해서는 2 개 이상의 연속 된 공백에 민감합니다. gets()
다음에 puts()
이옵니다. 그러나 나는 무엇이 잘못되었는지를 모른다.
/*
BUG: MORE THAN 1 SPACES NEXT TO EACHOTHER. WHERE THE FIRST CHARACTER GOES?!!
Write a function that takes a string as an argument and removes the spaces from the string.
Test it in a program that uses a loop to read lines until you enter an empty line.
The program should apply the function to each input string and display the result.
*/
#include <stdio.h>
#include <string.h>
int spaceRemover(char *in);
void takeBack(char *in);
int main(void)
{
puts("Enter a string for the SPACE-REMOVER: (RETURN to quit)");
do
{
char str[500];
int spaces;
gets(str);
puts(str); //for debugging to know is it complete just after gets() ?
//printf("\nFirst char of string: %c\n",str[0]);
//printf("\nFirst Char: %p '%c'\n",str,*str);
spaces=spaceRemover(str);
printf("\n%d spaces removed: \n",spaces);
puts(str);
puts("\nEnter a string for the SPACE-REMOVER: (RETURN to quit)");
}
while (getchar() != '\n');
return 0;
}
int spaceRemover(char *in)
{
int count=0, i;
for (i=0 ; i<strlen(in) ; i++)
while (*(in+i)==' ') //IF will skip more than one space; but WHILE won't
{
//printf("%p '%c' \t B4: %p '%c'\n",in+i,*(in+i),in+i-1,*(in+i-1));
takeBack(in+i);
count++;
}
return count;
}
void takeBack(char *spaceLocation)
{
int j=0;
while (*(spaceLocation+j)!= '\0')
{
*(spaceLocation+j) = *(spaceLocation+j+1);
//putchar(*(spaceLocation+j+1));
j++;
}
return;
}
출력 :
Enter a string for the SPACE-REMOVER: (RETURN to quit)
this is separated by single spaces
this is separated by single spaces
5 spaces removed:
thisisseparatedbysinglespaces
Enter a string for the SPACE-REMOVER: (RETURN to quit)
I'll try more than single space separators
'll try more than single space separators
13 spaces removed:
'lltrymorethansinglespaceseparators
참고 :이 하나를 사용하여 인용구를 사용하여이 연속 공간을 폐기 그래서 전체 코드를 인용.
여기에 무슨 일이 있습니까? 내 코드에 문제가 있습니까?
(GCC와 코드 :: 블록을 사용.) 문자열의 첫 번째 문자가가는
디버거가 작동하는 데 걸리는 시간. 모든 코드를 게시하고 "도움"을 요청하는 것은 버그가 수정 된 방법이 아닙니다. –
감사합니다. 나는 프로그래머를 얻는 것에 대한 어떤 조언도 환영한다. 나는 GDB를 시작하는 것으로 생각한다. – pedyram