본문 바로가기
C 언어

C언어로 문자열 Split 하기

by treeCoder 2021. 9. 19.

C언어로 문자열 Split 하여 Join 하는 예제이다.

개인적으로 만들어 본 것이라 사진을 첨부하면, 나중에 기억하기 좋을 것 같아서, 사진을 먼저 첨부한다.

문자열 연결은 sprintf를 사용하였다.

 

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

int main () {

   char fileNames[][100] = {
      "C:\\tcc\\1\\Peanuts - School Chums (a.en).srt",
      "C:\\tcc\\10\\Peanuts - Christmas Is On Its Way (a.en).srt",
      "C:\\tcc\\100\\Peanuts - Classic (a.en).srt",
      "C:\\tcc\\101\\Peanuts - Birds of a Feather (a.en).srt",
      "C:\\tcc\\102\\Peanuts - The Call of the Wild (a.en).srt",
      "C:\\tcc\\103\\Peanuts - Even the Score (a.en).srt",
      "C:\\tcc\\11\\Peanuts - Frieda (a.en).srt",
      "C:\\tcc\\12\\Peanuts - A Day With Snoopy (a.en).srt",
      "C:\\tcc\\13\\Peanuts - Technical Difficulties (a.en).srt",
      "C:\\tcc\\14\\Peanuts - Nobody Likes Me (a.en).srt",
      "C:\\tcc\\15\\Peanuts - A Strong Personality (a.en).srt",
      "C:\\tcc\\16\\Peanuts - The Eye of the Beholder (a.en).srt",
      "C:\\tcc\\17\\Peanuts - Miss Othmar (a.en).srt",
      "C:\\tcc\\18\\Peanuts - The School of Hard Knocks (a.en).srt",
      "C:\\tcc\\19\\Peanuts - My Blanket (a.en).srt",
      "C:\\tcc\\2\\Peanuts - Even-ing the Score (a.en).srt",
      "C:\\tcc\\20\\Peanuts - Novel Idea (a.en).srt",
      "C:\\tcc\\21\\Peanuts - No Strings Attached (a.en).srt",
      "C:\\tcc\\22\\Peanuts - A Friend Indeed (a.en).srt",
      "C:\\tcc\\23\\Peanuts - Black and White (a.en).srt",
      "C:\\tcc\\24\\Peanuts - Crazy Mutt (a.en).srt",
      "C:\\tcc\\25\\Peanuts - Beauty Contest (a.en).srt",
      "C:\\tcc\\26\\Peanuts - Being Nice (a.en).srt",
      "C:\\tcc\\27\\Peanuts - Telling Stories (a.en).srt",
      "C:\\tcc\\28\\Peanuts - A New Best Friend (a.en).srt",
      "C:\\tcc\\29\\Peanuts - Back to School (a.en).srt",
      "C:\\tcc\\3\\Peanuts - Dont Worry Charlie Brown (a.en).srt",
      "C:\\tcc\\30\\Peanuts - Winter Wonderland (a.en).srt",
      "C:\\tcc\\31\\Peanuts - Doing It Right (a.en).srt",
      "C:\\tcc\\32\\Peanuts - Autumn Is Here (a.en).srt",
      "C:\\tcc\\33\\Peanuts - Like Skates on Ice (a.en).srt",
      "C:\\tcc\\34\\Peanuts - Tennis (a.en).srt",
      "C:\\tcc\\35\\Peanuts - School Is Out (a.en).srt",
      "C:\\tcc\\36\\Peanuts - Sally (a.en).srt",
      "C:\\tcc\\37\\Peanuts - Not Your Day (a.en).srt",
      "C:\\tcc\\38\\Peanuts - A Hard Winter (a.en).srt",
      "C:\\tcc\\39\\Peanuts - A Little Nap (a.en).srt",
      "C:\\tcc\\4\\Peanuts - Have a Nice Day Linus (a.en).srt",
      "C:\\tcc\\40\\Peanuts - Word of Mouth (a.en).srt",
      "C:\\tcc\\41\\Peanuts - A Little Love (a.en).srt",
      "C:\\tcc\\42\\Peanuts - Love Is In The Air (a.en).srt",
      "C:\\tcc\\43\\Peanuts - On the Campaign Trail (a.en).srt",
      "C:\\tcc\\45\\Peanuts - A Clean Sweep (a.en).srt",
      "C:\\tcc\\46\\Peanuts - Brothers and Sisters (a.en).srt",
      "C:\\tcc\\47\\Peanuts - Marcie (a.en).srt",
      "C:\\tcc\\48\\Peanuts - Good Dog (a.en).srt",
      "C:\\tcc\\49\\Peanuts -  Kind of Spooky (a.en).srt",
      "C:\\tcc\\5\\Peanuts - The Little Red Haired Girl (a.en).srt",
      "C:\\tcc\\50\\Peanuts - Good for Nothing (a.en).srt",
      "C:\\tcc\\51\\Peanuts - Let It Snow (a.en).srt",
      "C:\\tcc\\52\\Peanuts - Security (a.en).srt",
      "C:\\tcc\\53\\Peanuts - Mans Best Friend (a.en).srt",
      "C:\\tcc\\54\\Peanuts - Its Just No Good (a.es).srt",
      "C:\\tcc\\55\\Peanuts - A Bad Mood (a.en).srt",
      "C:\\tcc\\56\\Peanuts - The Great Outdoors (a.en).srt",
      "C:\\tcc\\57\\Peanuts - Get Lucky (a.en).srt",
      "C:\\tcc\\58\\Peanuts - School Anxiety (a.en).srt",
      "C:\\tcc\\59\\Peanuts - Snoopy the Superstar (a.en).srt",
      "C:\\tcc\\6\\Peanuts - Good Sports (a.en).srt",
      "C:\\tcc\\60\\Peanuts - Show Dog (a.en).srt",
      "C:\\tcc\\61\\Peanuts - Off Key (a.en).srt",
      "C:\\tcc\\62\\Peanuts - Go for It Charlie Brown (a.en).srt",
      "C:\\tcc\\63\\Peanuts - Train Your Dog (a.en).srt",
      "C:\\tcc\\64\\Peanuts - Amateurs (a.en).srt",
      "C:\\tcc\\65\\Peanuts - A Fascinating Friend (a.en).srt",
      "C:\\tcc\\66\\Peanuts - A Pretty Picture (a.en).srt",
      "C:\\tcc\\67\\Peanuts - Keep Your Chin Up Charlie Brown (a.en).srt",
      "C:\\tcc\\68\\Peanuts - The Winter Games (a.en).srt",
      "C:\\tcc\\69\\Peanuts - Weird Little Brother (a.en).srt",
      "C:\\tcc\\7\\Peanuts - Sound & Sensibility (a.en).srt",
      "C:\\tcc\\70\\Peanuts - The Last of the Bunch (a.en).srt",
      "C:\\tcc\\71\\Peanuts - Too Cute (a.en).srt",
      "C:\\tcc\\72\\Peanuts - Leave Me in Peace (a.en).srt",
      "C:\\tcc\\73\\Peanuts - Misunderstanding (a.en).srt",
      "C:\\tcc\\74\\Peanuts - Not My Day (a.en).srt",
      "C:\\tcc\\75\\Peanuts - Its Only Love (a.en).srt",
      "C:\\tcc\\76\\Peanuts - Philosophy (a.en).srt",
      "C:\\tcc\\77\\Peanuts - Too Cold (a.en).srt",
      "C:\\tcc\\78\\Peanuts - Its Raining Its Pouring (a.en).srt",
      "C:\\tcc\\79\\Peanuts - Team Spirit (a.en).srt",
      "C:\\tcc\\8\\Peanuts - True or False (a.en).srt",
      "C:\\tcc\\80\\Peanuts - Just for Love (a.en).srt",
      "C:\\tcc\\81\\Peanuts -  That Day (a.en).srt",
      "C:\\tcc\\82\\Peanuts - Woodstock (a.en).srt",
      "C:\\tcc\\83\\Peanuts - Colors (a.en).srt",
      "C:\\tcc\\84\\Peanuts - Trust Me (a.en).srt",
      "C:\\tcc\\85\\Peanuts - School Tomorrow (a.en).srt",
      "C:\\tcc\\86\\Peanuts - Nice Doggy (a.en).srt",
      "C:\\tcc\\87\\Peanuts - Company (a.en).srt",
      "C:\\tcc\\88\\Peanuts - Reach for the Stars (a.en).srt",
      "C:\\tcc\\89\\Peanuts - Best Friend (a.en).srt",
      "C:\\tcc\\9\\Peanuts - The Mysteries of Love (a.en).srt",
      "C:\\tcc\\90\\Peanuts - Strike Out (a.en).srt",
      "C:\\tcc\\91\\Peanuts - Creepy Crawlies (a.en).srt",
      "C:\\tcc\\92\\Peanuts - Fly (a.en).srt",
      "C:\\tcc\\93\\Peanuts - Christmas is Coming (a.en).srt",
      "C:\\tcc\\94\\Peanuts - Beware of the Dog (a.en).srt",
      "C:\\tcc\\95\\Peanuts - Feeling Insecure (a.en).srt",
      "C:\\tcc\\96\\Peanuts - Always Right (a.en).srt",
      "C:\\tcc\\97\\Peanuts - Out of Order (a.en).srt",
      "C:\\tcc\\98\\Peanuts - Going My Way (a.en).srt",
      "C:\\tcc\\99\\Peanuts - Problems (a.en).srt"
    };


    int j;
    int size = sizeof( fileNames ) / sizeof( fileNames[0]);

    char *p;
    char buf[256];  

    for(int i = 0; i < size; i++ ) {
        buf[0]='\0';
        sprintf(buf, "%s%s%s%s", buf, "COPY   \"",  fileNames[i], "\"");
        sprintf(buf, "%-75s%s", buf, "  \"");
        
        for( j = 0, p = strtok(fileNames[i], "\\"); p!=NULL; j++ ) {
               if(j == 2) sprintf(buf, "%s%s\\%03d-",buf,  "C:\\tc1", strtol(p, NULL, 10));
               if(j > 2) sprintf(buf, "%s%s\\",buf,  p);
               p = strtok(NULL, "\\");   
        }
        if( (p = strrchr(buf, '\\')) ) *p = '\"'; // replace last back slash with double quatation
        printf(" %s \n", buf);
    }

    return 0;
}

댓글