문자열이 잇는데 공백에 의해 배열에 순차적으로 집어 넣는 함수

skylandi의 이미지

안녕 하세요
문자열이 잇는데 공백에 의해 배열에 순차적으로 집어 넣는 함수가 있는지요?

예를들면
안녕 하세요 저는 바보에요 ㅜㅜ

공백에 의해서

안녕
하세요
저는
바보에요
ㅜㅜ

배열에 저장하는 함수가 있는지좀 갈켜 주시면 감사하겟습니다.
없으면 만들어야 하는데 ㅠㅠ

errai의 이미지

C나 C++이라면 없습니다.
공백으로 구분해서 (strtok_r 등등 ) 직접 써주거나 (strncpy)
혹은 문자열 함수 조합에 따라 여러가지 방법이 있겠군요.

제 생각에는 루프돌면서 공백이 나올때마다
이차원 배열을 증가시켜 주면서 한 바이트씩 입력하는게 쉬울것 같군요.

위즐의 이미지

python에선 제목처럼 간단한데.. ㅋㅋ
c 라면 윗분말씀대로 루프돌리는게 작성도 쉽고 확장도 쉽고.. :)

cedar의 이미지

errai wrote:

C나 C++이라면 없습니다.
공백으로 구분해서 (strtok_r 등등 ) 직접 써주거나 (strncpy)
혹은 문자열 함수 조합에 따라 여러가지 방법이 있겠군요.

제 생각에는 루프돌면서 공백이 나올때마다
이차원 배열을 증가시켜 주면서 한 바이트씩 입력하는게 쉬울것 같군요.


글쎄요... 단지 공백(whitespace)만으로 구분할 때는 strtok()을 쓸 필요도 없습니다.
C에서는 sscanf(), C++에서는 istream::opeartor<<()와 istringstream 만으로 충분하지요.
ANSI C++의 STL의 istream_iterator를 사용하면 루프를 사용할 필요없이
다음과 같이 함수 호출의 형태로 쓸 수도 있습니다.
파이썬보다 많이 복잡하지는 않죠. ^^;

#include <iostream>
#include <sstream>
#include <iterator>
#include <vector>
using namespace std;

int main()
{
    istringstream is("안녕하세요 저는 바보에요 ㅜㅜ");
    vector<string> v;
    copy(istream_iterator<string>(is), istream_iterator<string>(),
    	 back_inserter(v));
    copy(v.begin(), v.end(), ostream_iterator<string>(cout, "\n"));
}
hurryon의 이미지

음냐. 참고가 될까 해서 소스을 하나 붙입니다. 시간을 구해서...시간의 각각의
구별자가 스페이스이기 때문에 스페이스을 이용해서 나눈것의 예입니다.

#include <stdio.h>
#include <string.h>
#include <time.h>

int main()
{
   char buf[32];
   char delims[] = {0x20, 0x20, 0x20, 0x20, 0x20}; /* 0x20 == space */
   char *token;

   time_t ltime;
   struct tm *newtime;


   time(&ltime);

   newtime = localtime(&ltime);

   snprintf(buf, 32, asctime(newtime));

   token = strtok(buf, delims);

   while(token != NULL)
   {
       printf("|%s|", token);
       token = strtok(NULL, delims);
   }

   return(0);
}


[hurryon@ns2 0320]$ gcc -o 4 4.c
[hurryon@ns2 0320]$ ./4
|Thu||Mar||20||13:21:28||2003
|[hurryon@ns2 0320]$

여기서 주의해서 보아야 할것은 문자열의 마지막에 자동으로 들어가 있는 \n 이다.
\n 이 들어 가 있기 때문에 마지막의 | 는 \n 이 나온 이후에 출력이 되었다.

vacancy의 이미지

Java라면 StringTokenizer class를 쓰시면 됩니다.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.