IT박스

C에서 두 개의 문자열을 어떻게 연결합니까?

itboxs 2020. 7. 11. 11:09
반응형

C에서 두 개의 문자열을 어떻게 연결합니까?


두 줄을 어떻게 추가합니까?

시도 name = "derp" + "herp";했지만 오류가 발생했습니다.

식은 정수 또는 열거 형이어야합니다


C는 일부 다른 언어에서 지원하는 문자열을 지원하지 않습니다. C의 문자열 char은 첫 번째 null 문자로 끝나는 배열에 대한 포인터 입니다. C에는 문자열 연결 연산자가 없습니다.

strcat두 개의 문자열을 연결하는 데 사용 합니다. 다음 기능을 사용하여 수행 할 수 있습니다.

#include <stdlib.h>
#include <string.h>

char* concat(const char *s1, const char *s2)
{
    char *result = malloc(strlen(s1) + strlen(s2) + 1); // +1 for the null-terminator
    // in real code you would check for errors in malloc here
    strcpy(result, s1);
    strcat(result, s2);
    return result;
}

이것이 가장 빠른 방법은 아니지만 지금은 걱정하지 않아도됩니다. 이 함수는 할당 된 힙 블록 메모리를 호출자에게 반환하고 해당 메모리의 소유권을 전달합니다. free메모리가 더 이상 필요하지 않은 경우 호출자가 메모리 를 책임집니다 .

다음과 같이 함수를 호출하십시오.

char* s = concat("derp", "herp");
// do things with s
free(s); // deallocate the string

성능에 방해가된다면, 널 종료자를 찾기 위해 입력 버퍼를 반복적으로 스캔하는 것을 피하고 싶을 것입니다.

char* concat(const char *s1, const char *s2)
{
    const size_t len1 = strlen(s1);
    const size_t len2 = strlen(s2);
    char *result = malloc(len1 + len2 + 1); // +1 for the null-terminator
    // in real code you would check for errors in malloc here
    memcpy(result, s1, len1);
    memcpy(result + len1, s2, len2 + 1); // +1 to copy the null-terminator
    return result;
}

문자열로 많은 작업을 할 계획이라면 문자열에 대해 일등을 지원하는 다른 언어를 사용하는 것이 좋습니다.


#include <stdio.h>

int main(){
    char name[] =  "derp" "herp";
    printf("\"%s\"\n", name);//"derpherp"
    return 0;
}

David Heffernan 은 그의 대답에서 문제를 설명 하고 개선 된 코드를 작성했습니다. 아래를 참조하십시오.

일반적인 기능

여러 문자열을 연결 하는 유용한 가변 함수작성할 수 있습니다.

#include <stdlib.h>       // calloc
#include <stdarg.h>       // va_*
#include <string.h>       // strlen, strcpy

char* concat(int count, ...)
{
    va_list ap;
    int i;

    // Find required length to store merged string
    int len = 1; // room for NULL
    va_start(ap, count);
    for(i=0 ; i<count ; i++)
        len += strlen(va_arg(ap, char*));
    va_end(ap);

    // Allocate memory to concat strings
    char *merged = calloc(sizeof(char),len);
    int null_pos = 0;

    // Actually concatenate strings
    va_start(ap, count);
    for(i=0 ; i<count ; i++)
    {
        char *s = va_arg(ap, char*);
        strcpy(merged+null_pos, s);
        null_pos += strlen(s);
    }
    va_end(ap);

    return merged;
}

용법

#include <stdio.h>        // printf

void println(char *line)
{
    printf("%s\n", line);
}

int main(int argc, char* argv[])
{
    char *str;

    str = concat(0);             println(str); free(str);
    str = concat(1,"a");         println(str); free(str);
    str = concat(2,"a","b");     println(str); free(str);
    str = concat(3,"a","b","c"); println(str); free(str);

    return 0;
}

산출:

  // Empty line
a
ab
abc

대청소

메모리 누수가 발생하지 않도록 필요에 따라 할당 된 메모리를 비워야합니다.

char *str = concat(2,"a","b");
println(str);
free(str);

strcat또는 더 나은 것을 사용해야합니다 strncat. Google it (키워드는 "연결")입니다.


일회성에 필요한 것으로 가정하겠습니다. PC 개발자 인 것으로 가정하겠습니다.

스택을 사용하세요, 루크 어디서나 사용하십시오. , malloc에 / 작은 할당에 대한 무료 사용하지 마십시오 이제까지 .

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

#define STR_SIZE 10000

int main()
{
  char s1[] = "oppa";
  char s2[] = "gangnam";
  char s3[] = "style";

  {
    char result[STR_SIZE] = {0};
    snprintf(result, sizeof(result), "%s %s %s", s1, s2, s3);
    printf("%s\n", result);
  }
}

문자열 당 10KB로 충분하지 않으면 크기에 0을 추가하고 신경 쓰지 마십시오. 어쨌든 스코프의 끝에서 스택 메모리를 해제합니다.


C에서와 같은 문자열 리터럴을 추가 할 수 없습니다. 문자열 리터럴 1 + 문자열 리터럴 2 + 널 종료 문자의 바이트 크기의 버퍼를 작성 하고 해당 리터럴을 해당 버퍼에 복사하고 널 종료인지 확인해야합니다. . 또는 같은 라이브러리 함수를 사용할 수 있습니다 strcat.


GNU 확장이없는 경우 :

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

int main(void) {
    const char str1[] = "First";
    const char str2[] = "Second";
    char *res;

    res = malloc(strlen(str1) + strlen(str2) + 1);
    if (!res) {
        fprintf(stderr, "malloc() failed: insufficient memory!\n");
        return EXIT_FAILURE;
    }

    strcpy(res, str1);
    strcat(res, str2);

    printf("Result: '%s'\n", res);
    free(res);
    return EXIT_SUCCESS;
}

또는 GNU 확장을 사용하는 경우 :

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
    const char str1[] = "First";
    const char str2[] = "Second";
    char *res;

    if (-1 == asprintf(&res, "%s%s", str1, str2)) {
        fprintf(stderr, "asprintf() failed: insufficient memory!\n");
        return EXIT_FAILURE;
    }

    printf("Result: '%s'\n", res);
    free(res);
    return EXIT_SUCCESS;
}

See malloc, free and asprintf for more details.


#include <string.h>
#include <stdio.h>
int main()
{
   int a,l;
   char str[50],str1[50],str3[100];
   printf("\nEnter a string: ");
   scanf("%s",str);
   str3[0]='\0';
   printf("\nEnter the string which you want to concat with string one: ");
   scanf("%s",str1);
   strcat(str3,str);
   strcat(str3,str1);
   printf("\nThe string is %s\n",str3);
}

Concatenate Strings

Concatenating any two strings in C can be done in atleast 3 ways :-

1) By copying string 2 to the end of string 1

#include <stdio.h>
#include <string.h>
#define MAX 100
int main()
{
  char str1[MAX],str2[MAX];
  int i,j=0;
  printf("Input string 1: ");
  gets(str1);
  printf("\nInput string 2: ");
  gets(str2);
  for(i=strlen(str1);str2[j]!='\0';i++)  //Copying string 2 to the end of string 1
  {
     str1[i]=str2[j];
     j++;
  }
  str1[i]='\0';
  printf("\nConcatenated string: ");
  puts(str1);
  return 0;
}

2) By copying string 1 and string 2 to string 3

#include <stdio.h>
#include <string.h>
#define MAX 100
int main()
{
  char str1[MAX],str2[MAX],str3[MAX];
  int i,j=0,count=0;
  printf("Input string 1: ");
  gets(str1);
  printf("\nInput string 2: ");
  gets(str2);
  for(i=0;str1[i]!='\0';i++)          //Copying string 1 to string 3
  {
    str3[i]=str1[i];
    count++;
  }
  for(i=count;str2[j]!='\0';i++)     //Copying string 2 to the end of string 3
  {
    str3[i]=str2[j];
    j++;
  }
  str3[i]='\0';
  printf("\nConcatenated string : ");
  puts(str3);
  return 0;
}

3) By using strcat() function

#include <stdio.h>
#include <string.h>
#define MAX 100
int main()
{
  char str1[MAX],str2[MAX];
  printf("Input string 1: ");
  gets(str1);
  printf("\nInput string 2: ");
  gets(str2);
  strcat(str1,str2);                    //strcat() function
  printf("\nConcatenated string : ");
  puts(str1);
  return 0;
}

In C, you don't really have strings, as a generic first-class object. You have to manage them as arrays of characters, which mean that you have to determine how you would like to manage your arrays. One way is to normal variables, e.g. placed on the stack. Another way is to allocate them dynamically using malloc.

Once you have that sorted, you can copy the content of one array to another, to concatenate two strings using strcpy or strcat.

Having said that, C do have the concept of "string literals", which are strings known at compile time. When used, they will be a character array placed in read-only memory. It is, however, possible to concatenate two string literals by writing them next to each other, as in "foo" "bar", which will create the string literal "foobar".

참고URL : https://stackoverflow.com/questions/8465006/how-do-i-concatenate-two-strings-in-c

반응형