C_CPP_STRING_PARSE - 8BitsCoding/RobotMentor GitHub Wiki

๋ชฉ์ฐจ

์ถ”๊ฐ€์‚ฌํ•ญ


C / C++ Style

  • C - char *
  • C++ = std::string
// include ๊ณตํ†ต์‚ฌํ•ญ
#include <stdio.h>      // for printf ...
#include <iostream>     // for cout ...
#include <string.h>     // for std::string ...

C string <-> C++ Sting

// C to C++
const char * cstr = "hello c";
std::string cppstr;
cppstr = std::string(cstr);

cout << cppstr.c_str();
// hello c

// C++ to C
std::string cppstr = "hello cpp";
const char * cstr =cppstr.c_str();

cout << cstr;
// hello cpp

๋ฌธ์ž์—ด ๋ณต์‚ฌ

// C
char cstr1[20] = "hello c";
char cstr2[20];
strcpy(cstr2, cstr1);

cout << cstr2;
// hello c

// C++
std::string cppstr1 = "hello cpp";
std::string cppstr2;
cppstr2 = cppstr1;

cout << cppstr2;
// hello cpp

๋ฌธ์ž ๊ฒ€์ƒ‰

// C
#include <string.h>         // for memchr
char search_char = 'h';
char * result_char;
char cstr[] = "hello cpp";
result_char = (char*)memchr(cstr, search_char, strlen(cstr));

if(result_char != NULL) {
    printf("'p' found at position %d.\n", result_char - cstr);
    // 'p' found at position 0
    // printf("%d\n", p);               // ์ฐพ์€ ๋ฌธ์ž์˜ ์ฃผ์†Œ๊ฐ’์„ ๋ฆฌํ„ด
}

// C++
char search_char = 'h';
std::string cppstr = "hello cpp";
std::size_t found = cppstr.find(search_char);

cout << found;
// 0

๊ฐœํ–‰๋ฌธ์ž ๊ฒ€์ƒ‰

// C
char search_char = '\n';
char * result_char;
char cstr[] = "hello\n cpp\n";
result_char = (char*)memchr(cstr, search_char, strlen(cstr));

if(result_char != NULL) {
    printf("'p' found at position %d.\n", result_char - cstr);
    // 'p' found at position 5
    // printf("%d\n", p);               // ์ฐพ์€ ๋ฌธ์ž์˜ ์ฃผ์†Œ๊ฐ’์„ ๋ฆฌํ„ด
}

// C++
char search_char = '\n';
std::string cppstr = "hello\n cpp\n";
std::size_t found = cppstr.find(search_char);

if(found != string::npos) {
    cout << found;
    // 5
}

๋ฌธ์ž์—ด ๊ฒ€์ƒ‰

// C
#include <string.h>         // for strstr
char cstr[] = "A Garden Diary";
char find_str[] = "den";
char * result_char = strstr(cstr, find_str);

if(result_char != NULL) {
  cout << result_char;
}
else {
  cout << "Can't find";
}

// den Diary

// C++
char * search_str = "hello";
std::string cppstr = "hello cpp";
std::size_t found = cppstr.find(search_str);
if(found != string::npos) {
    // world!๋ผ๋Š” ๋ฌธ์ž์—ด์„ ์ฐพ์•˜์„ ๊ฒฝ์šฐ
}
else {
  cout << Can't find";
}

๋ฌธ์ž์—ด ๋ง๋ถ™์ด๊ธฐ

// C
char cstr1[20] = "hello c";
char cstr2[20] = "hello c2";
strcat(cstr1, cstr2);

cout << cstr1;
// hello chello c2

// C++
std::string cppstr1 = "hello cpp";
std::string cppstr2 = "hello cpp2";
cppstr1 += cppstr2;

cout << cppstr1.c_str();
// hello cpphello cpp2

// Or
cppstr1.append(cppstr2);
// hello cpphello cpp2

์‹œ์ž‘/์ค‘๊ฐ„/๋ ๋ฌธ์ž ์ถ”๊ฐ€

์‹œ์ž‘/๋ ๋ฌธ์ž ์ถ”๊ฐ€

// C
char cstr1[20] = "hello c";
char cstr2[20] = "hello c2";
char cresult[40];
snprintf(cresult, 40, "%s %s", cstr1, cstr2);

cout << cresult;
// hello c hello c2

// Or
char cstr1[20] = "hello c";
char cstr2[20] = "hello c2";
strncat(cstr1, cstr2, strlen(cstr2));

cout << cstr1;
// hello chello c2

// C++
std::string cppstr1 = "hello cpp";
std::string cppstr2 = "hello cpp2";
std::string cppresult;
cppresult = cppstr1 + " " + cppstr2;

cout << cppresult;
// hello cpp hello cpp2

์ค‘๊ฐ„ ๋ฌธ์ž ์ถ”๊ฐ€

// C - ๋ฌธ์ž์‚ฝ์ž…
#include <string.h>

char cstr[100] = "1234567890";
char findc = '5';
char *result_cstr;
int n;

if (result_cstr = index(cstr,findc)) {
    n = strlen(result_cstr + 1) + 1;
    memmove(result_cstr + 2, result_cstr + 1, n);
    *(result_cstr+1) = 'a';
}

printf("cstr = %s\n", cstr);
// 12345a67890
// C
void insert(char * m, char * s, int n) {
    memmove(m+n+strlen(s), m+n+1, strlen(s));
    memmove(m+n, s, strlen(s));
}
char cstr1[20] = "hello c";
char cstr2[20] = "hello c2";
insert(cstr1, cstr2, 5);

cout << cstr1;
// hellohello c2c

// C++
std::string str="to be question";
std::string str2="the ";
std::string str3="or not to be";
std::string::iterator it;

// used in the same order as described above:
str.insert(6,str2);                 // to be (the )question
str.insert(6,str3,3,4);             // to be (not )the question
str.insert(10,"that is cool",8);    // to be not (that is )the question
str.insert(10,"to be ");            // to be not (to be )that is the question
str.insert(15,1,':');               // to be not to be(:) that is the question
it = str.insert(str.begin()+5,','); // to be(,) not to be: that is the question
str.insert (str.end(),3,'.');       // to be, not to be: that is the question(...)
str.insert (it+2,str3.begin(),str3.begin()+3); // (or )

๋ฌธ์ž์—ด ๋น„๊ต

// C
char cstr1[20] = "hello c";
char cstr2[20] = "hello c2";
if(strcmp(cstr1, cstr2) == 0)
{
    // ๊ฐ™์Œ
}
if(strcmp(cstr1, cstr2) > 0)
{
    // ๋‹ค๋ฆ„, ์‚ฌ์ „์ƒ ์•„๋ž˜
}

// C++
std::string cppstr1 = "hello cpp";
std::string cppstr2 = "hello cpp2";
if(cppstr1 == cppstr2)
{
    // ๊ฐ™์Œ
}
if(cppstr1 > cppstr2)
{
    // ์‚ฌ์ „์ƒ์˜ ์ˆœ์„œ๊ฐ€ ๋’ค
}
// Or
if(cppstr1.compare(cppstr2) == 0)
{
    // ๊ฐ™์Œ
}
if(cppstr1.compare(cppstr2) < 0)
{
    // ์‚ฌ์ „์ƒ์˜ ์ˆœ์„œ๊ฐ€ ์•ž
}

๋ฌธ์ž ๊ต์ฒด

// C
#include <string.h>

char cstr[100] = "12345e67890";
char findc = 'e';
char *result_cstr;

if (result_cstr = index(cstr,findc)) {
    *result_cstr = ' ';
}

printf("cstr = %s\n", cstr);
// 12345 67890

// C++
string replaceAll(const string &str, const string& pattern, const string &replace) {
    string result = str;
    string::size_type pos = 0;
    string::size_type offset = 0;

    while(pos = result.find(pattern, offset)!=string::npos) {
        result.replace(result.begin() + pos, result.begin() + pos + pattern.size(), replace);
        offset = pos + replace.size();
    }

    return result;
}

cout << replaceAll("aaabbbaaa", "aaa", "1");
// 1bbb1

๋ฌธ์ž์—ด ๊ต์ฒด

// C
char *replaceAll(char *s, const char *olds, const char *news) {
  char *result, *sr;
  size_t i, count = 0;
  size_t oldlen = strlen(olds); if (oldlen < 1) return s;
  size_t newlen = strlen(news);


  if (newlen != oldlen) {
    for (i = 0; s[i] != '\0';) {
      if (memcmp(&s[i], olds, oldlen) == 0) count++, i += oldlen;
      else i++;
    }
  } else i = strlen(s);


  result = (char *) malloc(i + 1 + count * (newlen - oldlen));
  if (result == NULL) return NULL;


  sr = result;
  while (*s) {
    if (memcmp(s, olds, oldlen) == 0) {
      memcpy(sr, news, newlen);
      sr += newlen;
      s  += oldlen;
    } else *sr++ = *s++;
  }
  *sr = '\0';

  return result;
}
char s[] = "๋ด‰์ˆญ์•„ ํ•™๋‹น! ๋ด‰์ˆญ์•„ ํ•™๋‹น! ๋ด‰์ˆญ์•„ ํ•™๋‹น! ๋ด‰์ˆญ์•„ ํ•™๋‹น!";
char *s2;

printf("์›๋ณธ: %s\n", s);

s2 = replaceAll(s, "๋ด‰์ˆญ์•„", "๋งน๊ตฌ");

๋ฌธ์ž์—ด ํŒŒ์‹ฑ

// C
char cstr[30] = "The Little Prince";
char * ptr = strtok(cstr, " ");

while(ptr != NULL) {
    printf("%s\n", ptr);
    ptr = strtok(NULL, " ");
}

// C++
std::string cppstr1 = "hello cpp";
std::string cppresult = cppstr1.substr(0, cppstr.find(' '));    // hello
// Or
string s = "C++ is an impressive language";
string::size_type pos = s.find_first_of(" .");

while(pos != string::npos) {
  pos = s.find_first_of(" .", pos + 1);
}
// C++
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
 
int main() {
	// your code goes here
	string s = "1 2 3 4 5";
	istringstream iss(s);
 
	do
	{
		string sub;
		iss >> sub;
		cout << sub << endl;
	} while(iss);
 
	return 0;
}
// C++
void LetterCount(string str) { 

  // code goes here   
  string str2;
  for(int i=0;i<str.length();i++)
  {
    // ์•ŒํŒŒ๋ฒณ๊ณผ ๊ณต๋ฐฑ์„ ์ œ์™ธํ•œ ๋ฌธ์ œ ์ œ๊ฑฐ
    if(isalpha(str[i])||str[i]==' ')
    {
      str2.append(str.substr(i,1));
    }
  }
  char* ch = (char*)str2.c_str();
  int repeats = 1;
  for(const char* pch=strtok(ch," ");pch;pch=strtok(NULL," "))
  {
    // do anything
    // pch์— ํŒŒ์‹ฑ๋œ ๋ฌธ์ž ์ˆœ์ฐจ์  ์ถœ๋ ฅ
  }    
}

sstream์— ๊ด€ํ•˜์—ฌ

#include <string>       // for std::string
#include <sstream>      // for std::stringstream
#include <iostream>     // for cout

std::stringstream ss;

ss << 100 << ' ' << 200;

int foo, bar;
ss >> foo >> bar;

std::cout << "foo : " << foo << '\n';        // foo : 100
std::cout << "bar : " << bar << '\n';        // bar : 200

// ...

ss.str("Example string");
std::string cppstr = ss.str();
std::cout << cppstr << '\n';

mem function

  • memchr : ๋ฉ”๋ชจ๋ฆฌ์— ํ•ด๋‹น ์บ๋ฆญํ„ฐ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธ
/* memchr example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char * pch;
  char str[] = "Example string";
  pch = (char*) memchr (str, 'p', strlen(str));
  if (pch!=NULL)
    printf ("'p' found at position %d.\n", pch-str+1);
  else
    printf ("'p' not found.\n");
  return 0;
}

'p' found at position 5.

  • memcmp : ๋ฌธ์ž์—ด์„ ๋น„๊ต
/* memcmp example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char buffer1[] = "DWgaOtP12df0";
  char buffer2[] = "DWGAOTP12DF0";

  int n;

  n=memcmp ( buffer1, buffer2, sizeof(buffer1) );

  if (n>0) printf ("'%s' is greater than '%s'.\n",buffer1,buffer2);
  else if (n<0) printf ("'%s' is less than '%s'.\n",buffer1,buffer2);
  else printf ("'%s' is the same as '%s'.\n",buffer1,buffer2);

  return 0;
}

'DWgaOtP12df0' is greater than 'DWGAOTP12DF0'.

  • memcpy : ๋ฉ”๋ชจ๋ฆฌ๋ณต์‚ฌ
  • memmove : ๋ฉ”๋ชจ๋ฆฌ์ž˜๋ผ๋‚ด๊ธฐ
  • memset : ํŠน์ • ๊ฐ’์œผ๋กœ ๋ฉ”๋ชจ๋ฆฌ ์„ธํŒ…
/* memset example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] = "almost every programmer should know memset!";
  memset (str,'-',6);
  puts (str);
  return 0;
}

------ every programmer should know memset!


str function

  • strcat : ๋’ค๋กœ ์ŠคํŠธ๋ง ๋ณต์‚ฌ
/* strcat example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[80];
  strcpy (str,"these ");
  strcat (str,"strings ");
  strcat (str,"are ");
  strcat (str,"concatenated.");
  puts (str);
  return 0;
}

these strings are concatenated.

  • strchr : ํŠน์ • ๋ฌธ์ž ๊ฒ€์ƒ‰
/* strchr example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] = "This is a sample string";
  char * pch;
  printf ("Looking for the 's' character in \"%s\"...\n",str);
  pch=strchr(str,'s');
  while (pch!=NULL)
  {
    printf ("found at %d\n",pch-str+1);
    pch=strchr(pch+1,'s');
  }
  return 0;
}
Looking for the 's' character in "This is a sample string"...
found at 4
found at 7
found at 11
found at 18
  • strcmp : ๋ฌธ์ž์—ด ๋น„๊ต
#include <stdio.h>
#include <string.h>

int main ()
{
  char key[] = "apple";
  char buffer[80];
  do {
     printf ("Guess my favorite fruit? ");
     fflush (stdout);
     scanf ("%79s",buffer);
  } while (strcmp (key,buffer) != 0);
  puts ("Correct answer!");
  return 0;
}
Guess my favourite fruit? orange
Guess my favourite fruit? apple
Correct answer!
  • strcpy : ์ŠคํŠธ๋ง ๋ณต์‚ฌ
/* strcpy example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str1[]="Sample string";
  char str2[40];
  char str3[40];
  strcpy (str2,str1);
  strcpy (str3,"copy successful");
  printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3);
  return 0;
}
str1: Sample string
str2: Sample string
str3: copy successful
โš ๏ธ **GitHub.com Fallback** โš ๏ธ