反复呼叫strrchr ......找到倒数第二次出现 [英] repeated calls to strrchr... to find second to last occurence

查看:107
本文介绍了反复呼叫strrchr ......找到倒数第二次出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找到。的倒数第二次出现。字符串。


基本上我正在使用像
这样的网址 http://this.is.mydomin.com/path/to/file.txt


并且想要提取/path/to/file.txt


我以为我能够这样做:


---- ------------------------------------------------ <无线电通信/>
char * cd =(char *)NULL;


if(strstr(short_database," http")!=(char *)NULL){

cd = strrchr(short_database,''。'');

cd = strchr(cd,''/'');

strcpy( short_database,cd);

}

---------------------------- -----------------------


但是,因为有一个。在.txt中,这不起作用。

所以我需要重复调​​用才能找到第二个

最后一个。的实例。


任何人都可以提供帮助。在此先感谢,抱歉看起来很简单......我不是一个优秀的C程序员,

呢!

解决方案

Sean Berry< se ******** @ cox.net>潦草地写下以下内容:

我需要找到。的倒数第二次出现。在一个字符串中。
基本上我正在使用类似
的网址 http://this.is.mydomin.com/path/to/file.txt
并想要提取/path/to/file.txt
我以为我能够这样做:
---------------------------------------- ------------
char * cd =(char *)NULL;
if(strstr(short_database," http")!=(char *)NULL){
cd = strrchr(short_database,''。'');
cd = strchr(cd ,''/'');
strcpy(short_database,cd);
}
---------------------- -----------------------------
但是,因为有一个。在.txt中,这不起作用。
所以我需要重复调​​用才能找到第二个。的最后一个实例。
任何人都可以提供帮助。提前谢谢,抱歉看似简单的任务......我不是一个优秀的C程序员,




strrchr( )返回指向最后一个匹配的指针,如果有
不匹配则返回NULL。所以,如果它找到匹配,你需要调查最后一场比赛之前的字符串部分



首先检查指针是否与你的相同原字符串

指针。如果是,strrchr()在你的

字符串的确切开头找到了一个匹配,并且之前不可能有任何东西。所以在那个

的情况下,退出:没有倒数第二场比赛。

否则,改变strrchr()报告的位置中的字符

''\ 0'',从比赛开始切断字符串。然后再次打电话给

sttrchr()。如果找到匹配,那就是你的倒数第二个

匹配。否则退出:没有倒数第二场比赛。

如果你的字符串不可修改,请将其复制成可修改的字符串。


-

/ - Joona Palaste(pa*****@cc.helsinki.fi)-------------芬兰------ - $ \\

\ - http:// www .helsinki.fi / ~palaste ---------------------规则! -------- /

我们是女性。我们有双重标准可以实现。

- Ally McBeal




On Thu ,2004年6月24日,Sean Berry写道:


我需要找到。的倒数第二次出现。在字符串中。

基本上我正在使用像
http://this.is.mydomin.com/path/to/file.txt

想要提取/ path / to / file .txt




不要使用''strrchr''。正如你所发现的,那不行。

解决方案不是解决这个问题,而是以不同的方式解决问题。你要提取的字符串的哪一部分?

答案:下面的部分是mydomin.com。一般来说,

的部分紧跟在域名之后,这是一个

字母数字和点的序列,它本身跟在字符串http://之后;。

所以寻找http://,然后是一系列字母数字和

点,然后是斜线;然后从斜杠中提取所有内容

以后。


BTW,不必要的演员是邪恶的。


char * cd;

if(strncmp(short_database," http://",(sizeof" http://" -1))!= 0)

do_error(" URL不以''http://''!'开头);

cd = short_database +(sizeof" http://" -1);

while(!strchr(" /",* cd))

++ cd;

strcpy(short_database,cd);


(注意使用''strchr(" /",...)''而不是''(... ==''/'')''这个

是一个我发现非常有用的成语;它捕获了字符串结尾的

null字符以及我们所用的斜线真的很好看。

一定要在你的代码中保留这种行为;如果用户输入http:// www,你不想要
段错误。 google.com"!)

HTH,

-Arthur

2004年6月24日星期四,Sean Berry写道:

我需要找到倒数第二次出现的。。在字符串中。

基本上我正在使用像
http://this.is.mydomin.com/path/to/file.txt

想要提取/ path / to / file .txt


好​​像你问的是错误的问题。如果你想在域名之后提取

所有内容,那么寻找第二个''。''

字符并不总是有效。如果我有网址怎么办:

http://some.domain.com/path.with/ap..in/it/file.txt


Don''你想要所有的东西,包括第三个'​​/''?

我以为我能够这样做:

----- -----------------------------------------------
char * cd =(char *)NULL;

if(strstr(short_database," http")!=(char *)NULL){
cd = strrchr(short_database,' '。';;
cd = strchr(cd,''/'');
strcpy(short_database,cd);
}
------- --------------------------------------------
所以我需要重复调​​用才能找到第二个。的最后一个实例。

可以有人帮忙。提前谢谢,抱歉看似简单的任务......我不是一个优秀的C程序员,




你的C代码似乎不是问题。在尝试实现它之前,你可能想要跳转到

comp.programming并验证你的算法。


- -

发送电子邮件至:darrell at cs dot toronto dot edu

不要发送电子邮件至 vi ************ @ whitehouse.gov


I need to find the second to last occurence of a "." in a string.

Basically I am taking a URL like
http://this.is.mydomin.com/path/to/file.txt

and want to extract /path/to/file.txt

I thought I would be able to do it like this:

----------------------------------------------------
char *cd = (char *)NULL;

if (strstr(short_database, "http") != (char *)NULL) {
cd = strrchr(short_database, ''.'');
cd = strchr(cd, ''/'');
strcpy(short_database, cd);
}
---------------------------------------------------

But, since there is a "." in ".txt", this will not work.
So I need to repeat the call to find the second to
last instance of ".".

Can anyone help. Thanks in advance, and sorry about
the seemingly easy questing... I am not a good C programmer,
yet!

解决方案

Sean Berry <se********@cox.net> scribbled the following:

I need to find the second to last occurence of a "." in a string. Basically I am taking a URL like
http://this.is.mydomin.com/path/to/file.txt and want to extract /path/to/file.txt I thought I would be able to do it like this: ----------------------------------------------------
char *cd = (char *)NULL; if (strstr(short_database, "http") != (char *)NULL) {
cd = strrchr(short_database, ''.'');
cd = strchr(cd, ''/'');
strcpy(short_database, cd);
}
--------------------------------------------------- But, since there is a "." in ".txt", this will not work.
So I need to repeat the call to find the second to
last instance of ".". Can anyone help. Thanks in advance, and sorry about
the seemingly easy questing... I am not a good C programmer,
yet!



strrchr() returns a pointer to the last match, or NULL if there was
no match. So, if it found a match, you need to investigate the part
of the string that comes before the last match.
First check if the pointer is the same as your original string
pointer. If it is, strrchr() found a match at the exact start of your
string, and there can''t possibly be anything before it. So in that
case, exit: there isn''t a second-to-last match.
Otherwise, change the character in the position strrchr() reported to
''\0'', chopping off the string from the match onwards. Then call
sttrchr() again. If it found a match, that''s your second-to-last
match. Otherwise exit: there isn''t a second-to-last match.
If your string isn''t modifiable, copy it into a modifiable string.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"We''re women. We''ve got double standards to live up to."
- Ally McBeal



On Thu, 24 Jun 2004, Sean Berry wrote:


I need to find the second to last occurence of a "." in a string.

Basically I am taking a URL like
http://this.is.mydomin.com/path/to/file.txt

and want to extract /path/to/file.txt



Don''t use ''strrchr''. As you have discovered, that won''t work.
The solution is not to hack around the problem, but rather to solve
it a different way. What part of the string do you want to extract?
Answer: the part following "mydomin.com". In general, the part which
comes immediately after the domain name, which is a sequence of
alphanumerics and dots, which itself follows the string "http://".
So look for "http://", followed by a sequence of alphanumerics and
dots, followed by a slash; and then extract everything from the slash
onwards.

BTW, unnecessary casts are evil.

char *cd;
if (strncmp(short_database, "http://", (sizeof "http://" - 1)) != 0)
do_error("URL does not begin with ''http://''!");
cd = short_database + (sizeof "http://" - 1);
while (!strchr("/", *cd))
++cd;
strcpy(short_database, cd);

(Note the use of ''strchr("/",...)'' instead of ''(... == ''/'')''. This
is an idiom that I''ve found very useful; it catches the end-of-string
null character as well as the slash for which we''re really looking.
Be sure to preserve this behavior in your code; you don''t want to
segfault if the user enters "http://www.google.com"!)

HTH,
-Arthur


On Thu, 24 Jun 2004, Sean Berry wrote:

I need to find the second to last occurence of a "." in a string.

Basically I am taking a URL like
http://this.is.mydomin.com/path/to/file.txt

and want to extract /path/to/file.txt
It seems like you are asking the wrong question. If you want to extract
everything after the domain name then looking for the second last ''.''
character will not always work. What if I had the URL:

http://some.domain.com/path.with/a.p...in/it/file.txt

Don''t you want everything after, and including, the third ''/''?
I thought I would be able to do it like this:

----------------------------------------------------
char *cd = (char *)NULL;

if (strstr(short_database, "http") != (char *)NULL) {
cd = strrchr(short_database, ''.'');
cd = strchr(cd, ''/'');
strcpy(short_database, cd);
}
---------------------------------------------------

But, since there is a "." in ".txt", this will not work.
So I need to repeat the call to find the second to
last instance of ".".

Can anyone help. Thanks in advance, and sorry about
the seemingly easy questing... I am not a good C programmer,
yet!



Your C code doesn''t seem to be the problem. You might want to pop over to
comp.programming and validate your algorithm before you attempt to
implement it.

--
Send e-mail to: darrell at cs dot toronto dot edu
Don''t send e-mail to vi************@whitehouse.gov


这篇关于反复呼叫strrchr ......找到倒数第二次出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆