조회 수 38750 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

+ - Up Down Comment Print
?

단축키

Prev이전 문서

Next다음 문서

+ - Up Down Comment Print

1. Java 언어의 String.replace() 메쏘드와 유사한 기능의 C 함수입니다
2. Visual C/C++ 6.0 및 gcc에서 잘 동작하도록 작성되었습니다.
3. 메모리 누수(memory leak) 현상을 최대한 줄이려고 노력하였답니다.

출처: http://blog.empas.com/javaclue/22720841

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

#define FREE(x) if(x != NULL){free(x); x = NULL;}

/* --------------------------------------------------------
   replaceAll(char *srcStr, char *patStr, char *repStr)
    Convert a string to another string in which
    every substring, matched with the given pattern,
    is replaced to the given replacement substring.
    The source string is not changed.
   Parameters
    srcstr: the source string
    patstr: the pattern substring to be matched
    repstr: replacement substring
   Returns
    the new string.
   -------------------------------------------------------- */
char *replaceAll(char *srcstr, char *patstr, char *repstr)
{
 char *str = srcstr;
 char *idx;
 char *newstr = NULL;         // new string with replaced stuff
 char *tmpstr;                // temporary pointer to a string
 char *pstr = newstr;
 int replen = strlen(repstr);
 int patlen = strlen(patstr);
 int newlen = 0;
 int counter = 0;
 int sizealloc = 0;

 if (patlen < 1)
 {
  sizealloc = strlen(srcstr) + counter*(replen - patlen) + 1;
  tmpstr = (char *) realloc(newstr, sizealloc);

  if (tmpstr == NULL)
  {
   fprintf(stderr, "ERROR: realloc failed");
   if (newstr)
    free(newstr);
   newstr = NULL;
   return NULL;
  }

  newstr = tmpstr;
  newstr = strcpy(newstr, srcstr);
  return newstr;
 }

 while (idx = strstr(str, patstr))
 {
  counter++;
  str = idx + patlen;
 }

 if (counter > 0)
 {
  str = srcstr;
  sizealloc = strlen(srcstr) + counter*(replen - patlen) + 1;
  tmpstr = (char *) realloc(newstr, sizealloc);

  if (tmpstr == NULL)
  {
   fprintf(stderr, "ERROR: realloc failed");
   if (newstr)
    free(newstr);
   newstr = NULL;
   return NULL;
  }
  newstr = tmpstr;
  pstr = newstr;

  while (idx = strstr(str, patstr))
  {
   if (idx - str > 0)
   {
    newstr = strncat(newstr, str, idx - str);
   }
   newstr = strcat(newstr, repstr);
   str = idx + patlen;
  }
 }
 else
 {
  sizealloc = strlen(srcstr) + counter*(replen - patlen) + 1;
  tmpstr = (char *) realloc(newstr, sizealloc);

  if (tmpstr == NULL)
  {
   fprintf(stderr, "ERROR: realloc failed");
   if (newstr)
    free(newstr);
   newstr = NULL;
   return NULL;
  }
  newstr = tmpstr;
  newstr = strcpy(newstr, srcstr);
 }

 return newstr;
}


Dreamy의 코드 스크랩

내가 모으고 내가 보는

List of Articles
번호 분류 제목 날짜 조회 수 추천 수
116 MFC 리사이징 다이얼로그(Resizing dialog) 2008.03.24 47127 0
115 업무 마이크로 버블 Micro Bubble 이란 2016.08.29 9836 0
114 일반 마크다운 markdown (.md) 파일이란 2017.07.12 12765 0
113 LINUX 매번 잊는다.. tar 풀기 : tar zxvf file 2 2009.04.14 37068 0
112 Android 맨날 까먹는 버튼 클릭 이벤트 핸들러 코드 2014.12.17 6253 0
111 LINUX 명령어 뒤의 옵션 자동완성 기능, complete 명령어 2015.01.06 9270 0
110 Pi 무선랜 wifi 설정 2015.10.01 7338 0
109 C# 문자열 검색 / 조작 2012.01.03 23886 0
108 C# 문자열 다루기 1 2012.04.06 18947 0
107 일반 배치파일(bat 파일) 명령어 사용법 2007.01.23 43426 61
106 일반 배치파일(bat)에서 for루프 사용법 2010.04.15 48094 0
105 일반 배치파일(bat)에서 날짜-시간을 파일 명으로 쓰기 2014.10.10 29595 0
104 일반 배치파일(bat)에서 명령 실행결과를 변수에 저장하는 방법 2017.07.05 19478 0
103 일반 범용 레지 스터(eax, ebx, ecx, edx, esi, edi, esp, ebp) 2019.06.03 10409 0
102 C# 변경된 파일을 감시하는 루틴 2013.10.30 12855 0
목록
Board Pagination ‹ Prev 1 ... 22 23 24 25 26 27 28 29 30 31 ... 34 Next ›
/ 34

나눔글꼴 설치 안내


이 PC에는 나눔글꼴이 설치되어 있지 않습니다.

이 사이트를 나눔글꼴로 보기 위해서는
나눔글꼴을 설치해야 합니다.

설치 취소

Designed by sketchbooks.co.kr / sketchbook5 board skin

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5