挣扎着弦乐 [英] struggling with strings
问题描述
大家好!
我正在尝试学习C编程,但我的java背景会阻止我的
大脑。
我有一本书很多功能,其中一些不能正常工作,所以我有机会通过校正在C中锻炼
(奇怪,是吧?)。 />
例如,我想编写一个函数,从字符串中删除给定子串的所有出现的
。
到目前为止我有两个函数:
1.从字符串中删除第一个子字符串(我在这本可怕的书中对函数进行了
更正之后做了一遍:
char * strstr_rem_first(char * string,char * substring){
int i,j,k,loc = -1;
for(i = 0; string [i]&&(loc == - 1); i ++){
for(j = i,k = 0; string [j] == substring [k]; j ++,k ++)
if(!substring [k + 1])
loc = i;
if (loc!= -1){//找到子串
for(k = 0; substring [k]; k ++); {
for(j = loc,i = loc + k;串[I]; j ++,i ++){
string [j] = string [i];
string [i] =''\''';
}
}
}
}
返回(字符串);
}
2.找到子串的最右边索引(如果找不到则返回-1):
int substring_index(const char * string,const char * substring){
int i,j,k;
for(i = 0; string [i]; i ++)
for(j = i,k = 0; string [j] == substring [k]; j ++,k ++)
if(!substring [k + 1])
返回(i);
返回(-1);
}
现在,武装这些我想构建删除所有
子串的函数。
我虽然有类似这样的事情:
while(substring_index()> 0)
string_original变为substring_rem_first
返回string_original
不幸的是,因为字符串被视为数组,我不能将
转移引用作为e我已经习惯了,我无法弄清楚如何在C中完成它。
我拖着一些指针和一些strcpy()等徒劳无功。
特别是这个''string_original变成substring_rem_first''困扰
我。
我总是以''undefined''引用''结束'' substring_rem_first''。
你能救我吗? :-)
Hi guys!
I am trying to learn C programming but my java background blocks my
brains.
I have a book with a lot of functions and some of them don''t work
correctly, so I have opportunity to exercise in C by correction
(strange, huh?).
For example, i want to write function that removes all occurences of
given substring from string.
I have two functions so far:
1. removes first substring from the string (i made it after
corrections of the function that i had in the horrible book:
char *strstr_rem_first(char *string, char *substring) {
int i, j, k, loc=-1;
for(i=0; string[i] && (loc==-1); i++) {
for(j=i, k=0; string[j] ==substring[k];j++, k++)
if(!substring[k+1])
loc=i;
if(loc != -1) { //substring was found
for(k=0; substring[k];k++); {
for(j=loc, i=loc+k; string[i]; j++, i++) {
string[j] = string[i];
string[i]=''\0'';
}
}
}
}
return (string);
}
2. finds rightmost index of the substring (returns -1 if not found):
int substring_index(const char *string, const char *substring) {
int i, j, k;
for(i=0; string[i];i++)
for(j=i,k=0; string[j] == substring[k]; j++, k++)
if(!substring[k+1])
return(i);
return(-1);
}
Now, armed with these I wanted to construct function that removes all
substrings.
I though about somthing like this:
while(substring_index() > 0)
string_original becomes substring_rem_first
return string_original
Unfortunately, because strings are treated as arrays and i can''t
transfer references as easily as i got used to, i can''t figure out how
to accomplish it in C.
I shuffled some pointers and some strcpy() etc in vain.
Especially this ''string_original becomes substring_rem_first'' bothers
me.
I always end up with ''undefined'' reference to ''substring_rem_first''.
Could you help me out? :-)
推荐答案
你好tuchka,
" tuchka" < IA *** @ hotmail.com> schrieb im Newsbeitrag
新闻:5f ************************** @ posting.google.c om ...
Hi tuchka,
"tuchka" <ia***@hotmail.com> schrieb im Newsbeitrag
news:5f**************************@posting.google.c om...
1.从字符串中删除第一个子字符串(我在可怕的书中对函数进行了更正后进行了修改:
2.查找子字符串的最右边索引(如果没有找到则返回-1):
1. removes first substring from the string (i made it after
corrections of the function that i had in the horrible book:
2. finds rightmost index of the substring (returns -1 if not found):
查看在string.h中声明的函数。:-)
这里是删除所有substring的函数。来自string:
#include< string.h>
char * strstr_rem_all(char * string,const char * substring ){
char *发生; int substr_len = strlen(substring); int move_len =
strlen(string) - substr_len + 1; / * +1 b / c终止''\ 0''* /
while(occurrence = strstr(字符串,子串)){
memmove(发生, string + substr_len,move_len);
}
返回字符串;
}
memmove( )用来代替memcpy(),因为它处理重叠的
副本。 :-)
我希望有所帮助! :-)
问候,
Ekkehard Morgenstern。
Check out the functions declared in "string.h". :-)
Here''s a function to remove all occurrences of "substring" from "string":
#include <string.h>
char* strstr_rem_all( char* string, const char* substring ) {
char* occurrence; int substr_len = strlen( substring ); int move_len =
strlen( string ) - substr_len + 1; /* +1 b/c of terminating ''\0'' */
while ( occurrence = strstr( string, substring ) ) {
memmove( occurrence, string + substr_len, move_len );
}
return string;
}
memmove() is used instead of memcpy(), because it handles overlapping
copies. :-)
I hope that helps! :-)
Regards,
Ekkehard Morgenstern.
谢谢你,Ekkehard,
我看到这个功能怎么可能有效但我仍然无法得到
工作。
你写的函数给出了以下内容错误:
C:\ Sveta \ C_PROG~1 \ STRING~1> str_test
123fart456fart789fart
反向traf987traf654traf321是traf987traf654traf321
与X填充成为xxxxxxxxxxxxxxxxxxxxx
除去第一屁后成为123456fart789fart
移除所有屁成了
后
123art456rt456rt456rt456rt456rt456rt456rt?
在EIP = 0000772e
EAX = 00000000 EBX退出由于信号SIGSEGV
一般保护性错误= 00000000 ECX = FFFFFFFF EDX = FFFFFFFF ESI = FFFFFFFF
EDI = FFFFFFFF
EBP = 00090390 ESP = 0008ef6c
方案= C:\ SVETA \ C_PROG~1 \STRING~1 \STR_TEST.EXE >
cs:sel = 01a7 base = 029a0000 limit = 0009ffff
ds:sel = 01af base = 029a0000 limit = 0009ffff
es:sel = 01af base = 029a0000极限= 0009ffff
FS:SEL =拉丁文扩展-A基= 00005a50极限= 0000FFFF
GS:SEL = 01BF碱= 00000000极限= 0010ffff
ss:sel = 01af base = 029a0000 limit = 0009ffff
app stack:[00090560..00010560] Exceptn stack:[000104c0..0000e580]
电话帧追溯EIP:
0x0000772e
0x00003946
0x00001b82
0x000033d8
我像这样重写它但是我已经''未定义引用''
str_rem_first(string,substring):
char * strstr_rem_all(char * string,const char * substring){
// char occurence [strlen(string)];
char * ptr;
int substr_len = strlen(substring);
int move_len = strlen(string)-substr_len + 1;
while(ptr = strstr(string) ,substring)){
char occurence [strlen(string)];
memmove(occurence,str_rem_first(string,substring),
strlen(string)-substr_len + 1);
}
返回字符串;
}
叹息,感叹:-(
为什么郎。真是太难了?
Ekkehard Morgenstern < EK ****************** @ onlinehome.de>在留言新闻中写道:< bp ********** @ online.de> ...
Thank you, Ekkehard,
I see how that function could''ve work but i still cannot get it to
work.
The function as written by you gives the following error:
C:\Sveta\C_PROG~1\STRING~1>str_test
123fart456fart789fart
Reverse traf987traf654traf321 is traf987traf654traf321
Filled with x became xxxxxxxxxxxxxxxxxxxxx
After removing first fart became 123456fart789fart
After removing all fart became
123art456rt456rt456rt456rt456rt456rt456rt?
Exiting due to signal SIGSEGV
General Protection Fault at eip=0000772e
eax=00000000 ebx=00000000 ecx=ffffffff edx=ffffffff esi=ffffffff
edi=ffffffff
ebp=00090390 esp=0008ef6c
program=C:\SVETA\C_PROG~1\STRING~1\STR_TEST.EXE
cs: sel=01a7 base=029a0000 limit=0009ffff
ds: sel=01af base=029a0000 limit=0009ffff
es: sel=01af base=029a0000 limit=0009ffff
fs: sel=017f base=00005a50 limit=0000ffff
gs: sel=01bf base=00000000 limit=0010ffff
ss: sel=01af base=029a0000 limit=0009ffff
App stack: [00090560..00010560] Exceptn stack: [000104c0..0000e580]
Call frame traceback EIPs:
0x0000772e
0x00003946
0x00001b82
0x000033d8
I re-wrote it like this but again i''ve got ''undefined reference'' for
str_rem_first(string,substring):
char *strstr_rem_all(char *string, const char *substring) {
//char occurence[strlen(string)];
char *ptr;
int substr_len=strlen(substring);
int move_len=strlen(string)-substr_len+1;
while(ptr=strstr(string, substring)) {
char occurence[strlen(string)];
memmove(occurence, str_rem_first(string,substring),
strlen(string)-substr_len+1);
}
return string;
}
Sigh, sigh :-(
Why the lang. is so hard?
"Ekkehard Morgenstern" <ek******************@onlinehome.de> wrote in message news:<bp**********@online.de>...
嗨tuchka,
tuchka < IA *** @ hotmail.com> schrieb im Newsbeitrag
新闻:5f ************************** @ posting.google.c om ...
Hi tuchka,
"tuchka" <ia***@hotmail.com> schrieb im Newsbeitrag
news:5f**************************@posting.google.c om...
1.从字符串中删除第一个子字符串(我在可怕的书中对函数进行了更正后进行了修改:
2.找到子字符串的最右边索引(如果是,则返回-1)未找到):
1. removes first substring from the string (i made it after
corrections of the function that i had in the horrible book:
2. finds rightmost index of the substring (returns -1 if not found):
查看在string.h中声明的函数。:-)
这是一个删除所有出现的函数的函数"子"来自string:
#include< string.h>
char * strstr_rem_all(char * string,const char * substring){
char *发生; int substr_len = strlen(substring); int move_len =
strlen(string) - substr_len + 1; / * +1 b / c终止''\ 0''* /
while(occurrence = strstr(字符串,子串)){
memmove(occurrence,字符串+ substr_len,move_len);
}
返回字符串;
使用memmove()代替memcpy(),因为它处理重叠的
副本。 :-)
我希望有所帮助! :-)
问候,
Ekkehard Morgenstern。
Check out the functions declared in "string.h". :-)
Here''s a function to remove all occurrences of "substring" from "string":
#include <string.h>
char* strstr_rem_all( char* string, const char* substring ) {
char* occurrence; int substr_len = strlen( substring ); int move_len =
strlen( string ) - substr_len + 1; /* +1 b/c of terminating ''\0'' */
while ( occurrence = strstr( string, substring ) ) {
memmove( occurrence, string + substr_len, move_len );
}
return string;
}
memmove() is used instead of memcpy(), because it handles overlapping
copies. :-)
I hope that helps! :-)
Regards,
Ekkehard Morgenstern.
" Ekkehard Morgenstern" < EK ****************** @ onlinehome.de>在留言新闻中写道:< bp ********** @ online.de> ...
"Ekkehard Morgenstern" <ek******************@onlinehome.de> wrote in message news:<bp**********@online.de>...
嗨tuchka,
tuchka < IA *** @ hotmail.com> schrieb im Newsbeitrag
新闻:5f ************************** @ posting.google.c om ...
Hi tuchka,
"tuchka" <ia***@hotmail.com> schrieb im Newsbeitrag
news:5f**************************@posting.google.c om...
1.从字符串中删除第一个子字符串(我在可怕的书中对函数进行了更正后进行了修改:
2.找到子字符串的最右边索引(如果是,则返回-1)未找到):
检查在string.h中声明的函数。:-)
这是一个删除所有substring的函数。来自string:
#include< string.h>
char * strstr_rem_all(char * string,const char * substring){
char *发生; int substr_len = strlen(substring); int move_len =
strlen(string) - substr_len + 1; / * +1 b / c终止''\ 0''* /
while(occurrence = strstr(字符串,子串)){
memmove(occurrence,字符串+ substr_len,move_len);
}
返回字符串;
使用memmove()代替memcpy(),因为它处理重叠的
副本。 : - )
1. removes first substring from the string (i made it after
corrections of the function that i had in the horrible book:
2. finds rightmost index of the substring (returns -1 if not found):
Check out the functions declared in "string.h". :-)
Here''s a function to remove all occurrences of "substring" from "string":
#include <string.h>
char* strstr_rem_all( char* string, const char* substring ) {
char* occurrence; int substr_len = strlen( substring ); int move_len =
strlen( string ) - substr_len + 1; /* +1 b/c of terminating ''\0'' */
while ( occurrence = strstr( string, substring ) ) {
memmove( occurrence, string + substr_len, move_len );
}
return string;
}
memmove() is used instead of memcpy(), because it handles overlapping
copies. :-)
这里有点奇怪。
小心你要动多少......最初的长度
字符串 - 子串+1的长度。这不会在
循环内部发生变化,但每次字符串变短时都会发生变化。
这个怎么样?它应该工作。
char * strstr_rem_all(char * string,const char * substring){
char * occurrence;
int substr_len = strlen(substring);
int move_len;
while(occurrence = strstr(string,substring)){
move_len = strlen (发生) - substr_len + 1;
memmove(发生,发生+ substr_len,move_len);
}
返回字符串;
}
我希望有所帮助! :-)
问候,
Ekkehard Morgenstern。
Isn''t there something a bit weird here.
Be careful abt how much you are moving ... the length of the initial
string - that of the substring +1 . This does not change inside the
loop but each time the string gets shorter.
What about this ? It should work.
char* strstr_rem_all( char* string, const char* substring ) {
char* occurrence;
int substr_len = strlen( substring );
int move_len;
while ( occurrence = strstr( string, substring ) ) {
move_len = strlen( occurrence ) - substr_len + 1;
memmove( occurrence, occurrence + substr_len, move_len );
}
return string;
}
I hope that helps! :-)
Regards,
Ekkehard Morgenstern.
这篇关于挣扎着弦乐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!