포인터 배열을 사용해서 토큰을 출력하려고 하는데 잘 안됩니다 ㅠㅠ

kykk0010의 이미지

#include
#include
#include

int main(void)
{
char s1[50];
char s2[10][50];
char *token[10];
char *context;
int i = 0;

printf("문자열을 입력하시오:");
gets_s(s1);

token[0] = strtok(s1," ");

while(token !=NULL)
{
printf("토큰 :%s\n", token[i]);
token[i] = strtok(NULL," ");
i++;
}

return 0;

}

도대체 어떻게 된 건지 이유를 모르겠네요 ㅠㅠ

익명 사용자의 이미지

돌려보지는 않았는데 두 군데 잘못된 것 같네요. 첫번째는 while 조건 문에서 token은 token[i]여야 할 것 같고 i++는 그 앞 문장보다 먼저 와야 할 것 같습니다.

kykk0010의 이미지

그래도 안되네요 ㅠㅠ

raymundo의 이미지

"그래도"라고 쓰지 말고 자기가 수정한 코드를 정확히 적으세요. 질문자가 정말 제대로 수정했는지 아닌지 답변하는 사람이 확인하도록.
"안되네요"라고 쓰지 말고 도대체 뭐가 어떻게 안 된다는 건지 적으세요. 컴파일 에러가 나는 건지, 실행할 때 오류가 나는 건지, 오류 메시지는 무엇인지, 오류는 안 나는데 동작이 원하는 대로 안 된다면 어떤 입력을 주었는지.

저는 윗분의 답글대로 고쳐서 아주 잘 되는 걸 확인했는데, 저는 gcc 를 쓰느라 gets_s 를 gets 로 바꿔썼습니다.
gets_s 는 인자로 버퍼의 크기도 같이 주어야 할 텐데, gets_s( s1, 50 ) 이런 식으로 한 번 고쳐보시죠.

좋은 하루 되세요!

익명 사용자의 이미지

제가 그동안 잘못 답글을 달았었네요 ㅠㅠ 죄송합니다

다행히 아래 주석에 달린 것과 같이 수정해서 잘 작동되었습니다!

#include
#include
#include

int main(void)
{
char s1[50];
char *token[10];
char *context;
int i = 0;
int j ;
printf("문자열을 입력하시오:");
gets_s(s1);

token[0] = strtok_s(s1," ",&context);

while(token[i] != NULL)
{

i++; \\반복구문에서 i값을 먼저 증가시킴

token[i] = strtok_s(NULL," ",&context);

}
printf("%d\n", i);

j= i;

for(i= j-1 ; i>=0 ; i--)
printf("%s ", token[i]);

return 0;

}

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.