LinuxForum.Fans
Тема

С поиск позиции подстроки

Автор: tmp13 4 сообщений 1460 просмотров
#98116

Толи я искать не умею толи асфальт кривой..
но не понимаю как найти номер позиции подстроки в строке...
Максимум что нашёл:

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

int main () { char str[] ="This is a line"; char *p;

p = strstr (str, "is"); strncpy (p, "sample",5 );

return 0; }

но походу данный код не возвращает номер позиции.

почитал по аналогии с С++ вроде можно сделать как:

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

int main () { char str[] ="This is a line"; char *p; size_t found;

found=strstr (str, "is"); printf("Found: %i", int(found));

return 0; }

так конечно не работает.. ругается на int(found).. но как привести size_t к int в С я не понимаю..

#98132

Проблемма решена:

text
int inStr(char *pattern, char *str) {
        int i, foundPos = 0;

/* stop looping at end of string */ while(str[foundPos]!=0) { if (str[foundPos]==pattern[0]) { /* our current search position matches the first character in the pattern, we'll look for more */

i = 1; /* we know i=0 is a match, so we start at 1 we loop until one of one of three conditions is met 1. we hit the end the pattern 2. we hit the end of the string 3. the characters are not equal */ while(pattern[i]!=0 && str[foundPos + i]!=0 && str[foundPos + i]==pattern[i]) { i++; } /* if we got the end of the pattern, we have a match */ if (pattern[i]==0) { return foundPos;

/* if we got to the end of the search string, all other searches will fail too */ } else if (str[foundPos + i]==0) { return -1; } /* if it just didn't match, we move on */ } /* move our search one position forward and repeat */ foundPos++; } /* if we got here, we found nothing */

return -1; }

взято от седа: http://www.dreamincode.net/code/snippet2227.htm

#98210
tmp13 написал:
Толи я искать не умею толи асфальт кривой..
но не понимаю как найти номер позиции подстроки в строке...
Максимум что нашёл:
text
#include <stdio.h>
#include <string.h>

int main () { char str[] ="This is a line"; char *p;

p = strstr (str, "is"); strncpy (p, "sample",5 );

return 0; }

но походу данный код не возвращает номер позиции.
...

По ходу возвращает...
1 - char str[] ="This is a line\0";
2 - Под отладчиком запустите...

#98691

это очень медленно.
http://drbatty.ru/sed/ch02s04.html

text
#include <locale.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <regex.h>

#define ERRBUF_SIZE 1024 #define NMATCH 10

void error(int err_code, regex_t *rs)1 { char errbuf[ERRBUF_SIZE]; if(!err_code) return; regerror(err_code, rs, errbuf, ERRBUF_SIZE); fprintf(stderr, "regex error: %s\n", errbuf); regfree(rs); _exit(err_code); }

int main(void) { const char str[] = "тестов\xD1\xD1ая строчка. ABCDEFG";2 const char regex[] = ".*";3 regex_t rs; regmatch_t pmatch[NMATCH]; char *lc = setlocale(LC_ALL, "ru_RU.UTF-8");4 if(!lc) return 77; printf("test string: `%s' (%s)\n", str, lc); int err = regcomp(&rs, regex, REG_EXTENDED); error(err, &rs); const char *ps = str; while(*ps)5 { err = regexec(&rs, ps, NMATCH, pmatch, 0); error(err, &rs); int j; const char *ps2 = NULL; for(j = 0; j < NMATCH; j++) { if(pmatch[j].rm_so < 0) continue; printf("Совпадение %d: %d...%d\t`", j, pmatch[j].rm_so, pmatch[j].rm_eo); int i; for(i = pmatch[j].rm_so; i < pmatch[j].rm_eo; i++) printf("%c", ps[i]); printf("'\n"); if(!ps2) ps2 = ps + i;6 } ps = ps2; if(!ps) break;7 printf("остаток: `%s'\n", ps); } regfree(&rs); return 0; }

Войдите или зарегистрируйтесь, чтобы ответить.