#include <stdio.h>
#include <string.h>
#include <conio.h>
int main (void)
{
char str[] = "This is a simple string";
char str2[] = "This is a simple simple string This is simple string simple simple simple";
char *pch;
char *pch2 = &str2;
int count;
int len;
pch = strstr (str, "simple");
if (!pch)
return -1;
strncpy (pch, "sample", 6);
puts (str);
pch = strstr (str, "sample");
if (!pch)
return -1;
strncpy (pch, "simple", 6);
puts (str);
count = 0;
len = strlen (pch2);
while (pch2 || len > 0) {
pch = strstr (pch2, "simple");
if (pch) {
++count;
len = len - (pch - pch2 + 6) ;
pch2 = pch + 6;
} else {
break;
}
}
printf ("Second sentens has %d simple\n\n", count);
printf ("Enter anykey for terminaing...");
_getch ();
return 0;
}
char * p = strstr (str, "문자열")
strstr은 str에서 동일한 문자열을 찾으면, p포인터를 넘겨준다.
point를 옮겨가면서, "simple" 개수도 찾아보았다.
--- 결과 ---
This is a sample string
This is a simple string
Second sentens has 6 simple
Enter anykey for terminaing...
'Programming > C,C++' 카테고리의 다른 글
문자열 비교 후 버블 소트 2 (0) | 2017.01.14 |
---|---|
문자열 비교 후 버블 소트 (0) | 2017.01.11 |
Bubble Sort (0) | 2017.01.10 |
ansi2uni / uni2ansi 예제 in Linux, Win32 (0) | 2017.01.09 |
Unicode에 관련하여[펌] (0) | 2017.01.09 |