子字符串在C不使用的功能 [英] Substring in c without using functions
问题描述
我已经看到了得到一个字符串的子串用strndup或的memcpy或函数strncpy等的使用许多解决方案,。
我不知道是否有一种方式来获得子不使用这些功能;即使是没有必要的。
编辑:我试图使自己的作用;我不记得什么问题了,但出了问题,我结束了不使用它。
的char *子(的char * str中,诠释开始,诠释长度){
字符* S =的malloc(sizeof的(字符)*(长+ 1)); 的for(int i =启动; I<启动+长;我++){
S [I-开始] = STR [I]
}
S [长度] ='\\ 0'; 返回S;
}
有多种方法来重新创建的strstr
。下面是使用英寸的蠕虫
方法,在这里您只需使用指针来搜索串$ C $年初的快速实现C>在
字符串
,然后一旦发现,每一个角色在串
与相应的字符字符串
。如果所有字符匹配,子被发现,在字符串
返回一个指针的开始串
。
如果字符未能通过测试,寻找另一个字符字符串
第一个字符匹配串
,直到字符串
已耗尽。
有可能是几个检查可以inplemented,但这个例子应该让你开始:
的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;字符* strstr2(的char * str中,字符*子)
{
如果(STR分||!)返回NULL; / *验证两个字符串* / 的char * p = NULL; / *一般指针* /
字符* SP = NULL; / *子指针* /
字符* RP = NULL; / *返回指针* /
CHAR匹配= 0; / *匹配的标志* /
为size_t szstr = 0; / *字符串长度* /
为size_t szsub = 0; / *子串的长度* / P =子;
而(* P ++)szsub ++; / * strlen的SUBSTR *的/ P =海峡;
而(* P ++)szstr ++; / * strlen的STR *的/ 如果(szsub> szstr)返回NULL; / * szstr< szsub - 不匹配* / P =海峡; 而(对≤(P + szstr - szsub + 1))
{
而(* P&放大器;&放大器;!* P = *子)p ++; / *找出str中*判的开始/ 如果((STR + szstr)== p)的返回NULL; / *如果年底达到了 - 没有分* / RP = P; / *保存返回指针* /
SP =子; / *设置SP分* /
匹配= 1; / * presume将匹配* /
而(* SP)/ *为每个子* /
如果(* P ++!= * SP ++){/ *检查是否匹配失败* /
匹配= 0; / *如果失败了,不匹配* /
打破; / *休息和放;找到新的开始* /
}
如果(匹配)/ *如果匹配,返回PTR * /
返回RP; / *开始在STR分* /
} 返回NULL; / *不匹配,返回NULL * /
}诠释主(){ 字符*字符串= NULL;
字符* SUBSTR = NULL;
字符*开始= NULL; 的printf(\\ n输入字符串:);
scanf函数(%M [^ \\ n]的%* C,&安培;字符串); 的printf(\\ n输入SUBSTR:);
scanf函数(%M [^ \\ n]的%* C,&安培; SUBSTR); 如果((开始= strstr2(字符串,SUBSTR))!= NULL)
的printf(\\ nSubstring发现在开头:%S \\ n \\ n开头);
其他
的printf(\\ nSubstring不是字符串\\ n \\ n); 在(string)免费(字符串);
如果(SUBSTR)免费(SUBSTR); 返回0;
}
输出:
$ ./bin/strstr输入字符串:这是一个完整的字符串或大海捞针。输入SUBSTR:G或子发现在开始:G或草垛。$ ./bin/strstr输入字符串:这是一个完整的字符串或大海捞针。输入SUBSTR:g或'在串不串。
I've seen many solutions for getting substring of a string with usage of strndup or memcpy or strncpy and etc,.
I was wondering if there's a way to get substring without using those functions; even if it's unnecessary.
EDIT: I tried making function myself; I don't remember what the problem was but something went wrong and I ended up not using it.
char *substring(char *str, int start, int length) {
char *s = malloc(sizeof(char)*(length+1));
for(int i=start; i<start+length; i++) {
s[i-start] = str[i];
}
s[length] = '\0';
return s;
}
There are a number of ways to recreate strstr
. The following is a quick implementation using the inch-worm
method, where you simply use pointers to search for the beginning of the substring
in string
, then if found, compare every character in substring
with the corresponding character in string
. If all characters match, the substring is found, return a pointer to the beginning of substring
in string
.
If a character fails the test, look for another character in string
that matches the first character in substring
, until string
is exhausted.
There are probably several more checks that can be inplemented, but this example should get you started:
#include <stdio.h>
#include <stdlib.h>
char *strstr2 (char *str, char *sub)
{
if (!str || !sub) return NULL; /* validate both strings */
char *p = NULL; /* general pointer */
char *sp = NULL; /* substring pointer */
char *rp = NULL; /* return pointer */
char matched = 0; /* matched flag */
size_t szstr = 0; /* string length */
size_t szsub = 0; /* substring length */
p = sub;
while (*p++) szsub++; /* strlen of substr */
p = str;
while (*p++) szstr++; /* strlen of str */
if (szsub > szstr) return NULL; /* szstr < szsub - no match */
p = str;
while (p < (p + szstr - szsub + 1))
{
while (*p && *p != *sub) p++; /* find start of sub in str */
if ((str + szstr) == p) return NULL; /* if end reached - no sub */
rp = p; /* save return pointer */
sp = sub; /* set sp to sub */
matched = 1; /* presume will match */
while (*sp) /* for each in substring */
if (*p++ != *sp++) { /* check if match fails */
matched = 0; /* if failed, no match */
break; /* break & find new start */
}
if (matched) /* if matched, return ptr */
return rp; /* to start of sub in str */
}
return NULL; /* no match, return NULL */
}
int main() {
char *string = NULL;
char *substr = NULL;
char *begin = NULL;
printf ("\nEnter string : ");
scanf ("%m[^\n]%*c", &string);
printf ("\nEnter substr : ");
scanf ("%m[^\n]%*c", &substr);
if ((begin = strstr2 (string, substr)) != NULL)
printf ("\nSubstring found beginning at : %s\n\n", begin);
else
printf ("\nSubstring NOT in string.\n\n");
if (string) free (string);
if (substr) free (substr);
return 0;
}
output:
$ ./bin/strstr
Enter string : This is the full string or "haystack".
Enter substr : g or "
Substring found beginning at : g or "haystack".
$ ./bin/strstr
Enter string : This is the full string or "haystack".
Enter substr : g or '
Substring NOT in string.
这篇关于子字符串在C不使用的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!