C에서 문자열 자르기

swakis의 이미지

문자열 "소나무1234567890"

있다면 앞의 "소나무" 와 숫자를 나누어 저장하고 싶습니다.
앞의 소나무는 유동적이구요. 뒤에숫자는 호출되어져서 정해져있습니다. 뒤에서부터 10개까지 짤라 내면 될것같은데...
C 로 표현이 되지 않네요..
고수님들 부탁드립니다.

progcom의 이미지

문자수를 세서 (strlen()) 10을 뺀 위치를 가지고 놀면 되겠네요. :)

fromdj의 이미지

int len;
char str[]="소나무1000000000";
char left[MAX_BUF];
char *right;
len=strlen(str);
if (len>10)
    right=str+len-10;
strcpy(left,str);
left[len-10]='\0';

printf("%s,%s\n",left,right);

^^ be cool ~
http://fromdj.pe.kr

fromdj의 이미지

int len;
len=strlen(str);
int value=atoi(str);
str[len-10]='\0';

^^ be cool ~
http://fromdj.pe.kr

moonzoo의 이미지

int main()
{
	char * input = "소나무1234567890";
	char output1[64];
	char output2[64];
	int  len = 10;

	snprintf(output1,sizeof(output1),"%-.*s",strlen(input)-len,input);
	snprintf(output2,sizeof(output2),"%s",input + strlen(input)-len);

	printf("<<%s>>\n",output1);
	printf("<<%s>>\n",output2);
}
acidd15의 이미지

regex라이브 러리를 사용하면 유연한 프로그램이 될듯..

xwizardx의 이미지

제 생각에 사용목적에 따라 틀려질것 같네요

글자와 숫자를 나누는것이라면 atoi()를 활용하고
특정 스트링에서 뒤의 글자를 처리하기 위해서라면 strlen()을 활용하면
해결이 될듯 하네요

swakis의 이미지

다양한 방법이 있군요..

그런데 '\0' 이부분이 이해가 되질 않는데..
무슨 말인가요? 초기화인가? NULL 인가요?

ooti의 이미지

swakis wrote:
다양한 방법이 있군요..

그런데 '\0' 이부분이 이해가 되질 않는데..
무슨 말인가요? 초기화인가? NULL 인가요?

`소나무` 다음에 \0 을 넣어서 문자열이 끝났음을 표시합니다.

항상 건강하고 행복하세요.

May the force be with you.

익명 사용자의 이미지

33

익명 사용자의 이미지

전 Win32완전 첨해 보는데요
근데 저에게 난감한 문제가 하나 생겼어요
파일 오픈창에서 파일을 선택할때 꼭 usb구동기만이 인식되여 화일오픈창에 현시되여야 해요
여기 고수님들 좀 도와주세요 부탁합니다

a287848의 이미지

delimeter를 넣어서 strtok 같은 걸로 짤라 내는게 더 좋을 듯 해 보입니다

Dig it.

댓글 달기

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