c언어에서 문자열 변수에 문자를 추가시키고 싶은데.....

moonrepeat의 이미지

c언어 생 초보입니다.
c언어에서 문자열 변수에 문자를 추가 하고 싶습니다.

간단히 말해

char *c;
char d;

c="abcdef";
d='g';

마법을 일으킨다...........

printf("%s",c);

결과가 abcdefg 로 나오게 하고 싶은데
방법좀 알려주세요

불량도ㅐㅈㅣ의 이미지

Quote:

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

int main()
{
char str[] = "abcdef";
char one_word = 'g';
int len;

len = strlen(str);

str[len] = one_word;
str[len+1] = '\0';

printf("%s\n",str);

return 0;
}

꼭 문자열은 포인터로 선언해야 합니까?

배열로 쓰면 쉽게 풀리는데...

이렇게 해보면 원하는 출력이 될껍니다.

문근영 너무 귀여워~~

moonrepeat의 이미지

저.... 혹시 그러면 배열 값 전체에 NULL값을 주려면 어떻게 해야됩니까?

삽질은 계속되어야 한다....... 쭉.........

불량도ㅐㅈㅣ의 이미지

moonrepeat wrote:
저.... 혹시 그러면 배열 값 전체에 NULL값을 주려면 어떻게 해야됩니까?

전체에 NULL 값이요??

전체에 넣을 필요 있나요?

문자열이 NULL 값을 만나면 문장의 끝으로 인식 하는데....

문근영 너무 귀여워~~

ted78의 이미지

memset을 사용하면 됩니다.

char buf[50];
memset(buf, 0, sizeof(buf));

이런식으로 하면 buf[0]부터 buf[49]까지 0으로 초기화됩니다.

나는 생각하는 갈대다?

익명 사용자의 이미지

불량도ㅐㅈㅣ wrote:

char str[] = "abcdef";

...

str[len] = one_word;
str[len+1] = '\0';

printf("%s\n",str);

되기는 하겠지만,
char str[ENOUGH_SIZE] = "abcdef"; 나
malloc() 을 사용하는게 바람직하다고 생각합니다.

xster의 이미지

초기화할 때 0으로 만들려면 배열의 앞의 것만 초기화 하면 나머지는 모두 0으로 초기화되는 것을 사용하면 됩니다.
다음처럼 해주면 되겠네요.

char array[100] = { 0, };
krisna의 이미지

printf("%s%c",c,g);

moonrepeat의 이미지

krisna wrote:
printf("%s%c",c,g);

상큼한 답변입니다 ^^;

삽질은 계속되어야 한다....... 쭉.........

댓글 달기

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