// Cvoidinsert(char*m, char*s, intn) {
memmove(m+n+strlen(s), m+n+1, strlen(s));
memmove(m+n, s, strlen(s));
}
charcstr1[20] ="hello c";
charcstr2[20] ="hello c2";
insert(cstr1, cstr2, 5);
cout << cstr1;
// hellohello c2c// C++std::stringstr="to be question";
std::stringstr2="the ";
std::stringstr3="or not to be";
std::string::iteratorit;
// used in the same order as described above:str.insert(6,str2); // to be (the )questionstr.insert(6,str3,3,4); // to be (not )the questionstr.insert(10,"that is cool",8); // to be not (that is )the questionstr.insert(10,"to be "); // to be not (to be )that is the questionstr.insert(15,1,':'); // to be not to be(:) that is the questionit=str.insert(str.begin()+5,','); // to be(,) not to be: that is the questionstr.insert (str.end(),3,'.'); // to be, not to be: that is the question(...)str.insert (it+2,str3.begin(),str3.begin()+3); // (or )
/* memchr example */#include<stdio.h>#include<string.h>intmain ()
{
char*pch;
charstr[] ="Example string";
pch= (char*) memchr (str, 'p', strlen(str));
if (pch!=NULL)
printf ("'p' found at position %d.\n", pch-str+1);
elseprintf ("'p' not found.\n");
return0;
}
'p' found at position 5.
memcmp : ๋ฌธ์์ด์ ๋น๊ต
/* memcmp example */#include<stdio.h>#include<string.h>intmain ()
{
charbuffer1[] ="DWgaOtP12df0";
charbuffer2[] ="DWGAOTP12DF0";
intn;
n=memcmp ( buffer1, buffer2, sizeof(buffer1) );
if (n>0) printf ("'%s' is greater than '%s'.\n",buffer1,buffer2);
elseif (n<0) printf ("'%s' is less than '%s'.\n",buffer1,buffer2);
elseprintf ("'%s' is the same as '%s'.\n",buffer1,buffer2);
return0;
}
'DWgaOtP12df0' is greater than 'DWGAOTP12DF0'.
memcpy : ๋ฉ๋ชจ๋ฆฌ๋ณต์ฌ
memmove : ๋ฉ๋ชจ๋ฆฌ์๋ผ๋ด๊ธฐ
memset : ํน์ ๊ฐ์ผ๋ก ๋ฉ๋ชจ๋ฆฌ ์ธํ
/* memset example */#include<stdio.h>#include<string.h>intmain ()
{
charstr[] ="almost every programmer should know memset!";
memset (str,'-',6);
puts (str);
return0;
}
/* strchr example */#include<stdio.h>#include<string.h>intmain ()
{
charstr[] ="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');
}
return0;
}
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>intmain ()
{
charkey[] ="apple";
charbuffer[80];
do {
printf ("Guess my favorite fruit? ");
fflush (stdout);
scanf ("%79s",buffer);
} while (strcmp (key,buffer) !=0);
puts ("Correct answer!");
return0;
}
Guess my favourite fruit? orange
Guess my favourite fruit? apple
Correct answer!