편집 요약 없음 |
편집 요약 없음 |
||
33번째 줄: | 33번째 줄: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<syntaxhighlight lang="c"> | |||
#include <stdio.h> | |||
#include <string.h> | |||
int findStr (char *str, char *target) | |||
{ | |||
int i = 0; | |||
int j = 0; | |||
for ( ; (*target) != '\0' ; i++) | |||
{ | |||
int count = 0; | |||
for ( ; (*str) != '\0'; j++) | |||
{ | |||
if ( *str == *target) | |||
count++; | |||
str++; | |||
} | |||
str = str - j; | |||
printf("'%c' %d개", *target, count); | |||
target++; | |||
} | |||
printf("\n"); | |||
} | |||
int main () { | |||
char s[128]; | |||
strcpy (s, "Hello,World"); | |||
findStr(s, "Ho"); | |||
findStr(s, "ld"); | |||
findStr(s, "lod"); | |||
} | |||
</syntaxhighlight> | |||
'H' 1개'o' 2개 | |||
'l' 3개'd' 1개 | |||
'l' 3개'o' 2개'd' 1개 |
2021년 10월 15일 (금) 23:27 판
#include <stdio.h>
#include <string.h>
void delchars (char *str, char ch)
{
int len = strlen(str);
int i = 0;
while ( i < len )
{
if (str[i] == ch)
{
int j = i, width = 0;
if ( str[i+1] == ch)
width = 2;
else
width = 1;
while (j < len)
str[j] = str[j+width], j++;
}
i++;
}
}
int main ( )
{
char s1[128];
strcpy(s1, "HelloWorld");
delchars(s1, 'l');
printf("[%s]\n", s1); // [HeoWord]
delchars(s1, 'W');
printf("[%s]\n", s1); // [Heoord]
}
#include <stdio.h>
#include <string.h>
int findStr (char *str, char *target)
{
int i = 0;
int j = 0;
for ( ; (*target) != '\0' ; i++)
{
int count = 0;
for ( ; (*str) != '\0'; j++)
{
if ( *str == *target)
count++;
str++;
}
str = str - j;
printf("'%c' %d개", *target, count);
target++;
}
printf("\n");
}
int main () {
char s[128];
strcpy (s, "Hello,World");
findStr(s, "Ho");
findStr(s, "ld");
findStr(s, "lod");
}
'H' 1개'o' 2개 'l' 3개'd' 1개 'l' 3개'o' 2개'd' 1개