遍历C字符串:得到一个字符串的最后一个字 [英] traversing C string: get the last word of a string

查看:546
本文介绍了遍历C字符串:得到一个字符串的最后一个字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你将如何得到一个字符串的最后一个单词,从'\\ 0'换行符开始到最右边的空间呢?例如,我能有这样的事情其中str可以分配一个字符串:

how would you get the last word of a string, starting from the '\0' newline character to the rightmost space? For example, I could have something like this where str could be assigned a string:

char str[80];
str = "my cat is yellow";

我将如何得到黄?

How would I get yellow?

推荐答案

事情是这样的:

char *p = strrchr(str, ' ');
if (p && *(p + 1))
    printf("%s\n", p + 1);

这篇关于遍历C字符串:得到一个字符串的最后一个字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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