您的位置:程序门 -> c/c++ ->



怎么做呢?想了好久了。都没做出来。。。


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


怎么做呢?想了好久了。都没做出来。。。
发表于:2007-11-02 16:04:16 楼主
编写一个程序,用于接收两个字符串,判断第一个字符串中是否包含第二个字符串,如果包含说明第二个字符串在第一个字符串中的位置(即,第二个字符串第一个字符在第一个字符串中出现的位置).         怎么做的?这个..
发表于:2007-11-02 16:04:551楼 得分:0
c库有个函数     strstr
发表于:2007-11-02 16:09:252楼 得分:0
我也才刚学c没多少久。。。
发表于:2007-11-02 16:12:123楼 得分:0
c/c++ code
const char * strstr ( const char * str1, const char * str2 ); char * strstr ( char * str1, const char * str2 ); locate substring returns a pointer to the first occurrence of str2 in str1, or a null pointer if there str2 is not part of str1. the matching process does not include the terminating null-characters. parameters str1 c string to be scanned. str2 c string containing the sequence of characters to match. return value a pointer to the first occurrence in str1 of any of the entire sequence of characters specified in str2, or a null pointer if the sequence is not present in str1. portability in c, this function is declared as: char * strstr ( const char *, const char * ); instead of the two overloaded versions provided in c++. example /* strstr example */ #include <stdio.h> #include <string.h> int main () { char str[] ="this is a simple string"; char * pch; pch = strstr (str,"simple"); strncpy (pch,"sample",5); puts (str); return 0; } this example searches for the "simple" substring in str and replaces that word for "sample". output: this is a sample string
发表于:2007-11-02 16:13:004楼 得分:0
头文件是   <string.h>   ,如果是c++,那就是   <cstring>
发表于:2007-11-02 16:32:065楼 得分:0
大哥。。大部分都是英文。。。我看不懂。。
发表于:2007-11-02 16:33:346楼 得分:0
这个   example   总能看的懂吧,它实现的就是你想要的功能啊
c/c++ code
example /* strstr example */ #include <stdio.h> #include <string.h> int main () { char str[] ="this is a simple string"; char * pch; pch = strstr (str,"simple"); strncpy (pch,"sample",5); puts (str); return 0; }
发表于:2007-11-02 16:36:367楼 得分:0
可是,,题目要求是要从键盘接收两个字符串,不能直接声明的吧?
发表于:2007-11-02 17:43:298楼 得分:0
strstr(string1,   string2)是查看string2串是否是string1串的子串,
如果是的话返回string2串在string1串中的第一个字符位置处的指针,
就是上面的pch   =   strstr   (str,"simple");
那个是个模板   你想要啥功能就该把!!!
发表于:2007-11-02 23:57:169楼 得分:0
看来我还得多多看书。。谢谢各位。


快速检索

最新资讯
热门点击