求大路大神指点

翀頨 2016-05-18 09:45:17
编写一个程序,判定一个字符串是否为另外一个字符串的子串,若是,返回子串在主串中的位置。要求不适用strstr函数,自己编写一个子函数实现。 C++ 不是C谢谢!!!!!!!
...全文
89 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
小灸舞 2016-05-18
  • 打赏
  • 举报
回复

#include<iostream>
#include<string>
#include<cstring>
using namespace std;

string a,b;

int main()
{
	int i,j,l,f;

	//输入两个字符串
	cin>>a>>b;
	for(i=0;i<a.length();i++)//遍历字符串a
	{
		//如果a的长度-i小于了b的长度,那么不需要再进行比较了
		if(a.length()-i<b.length()) break;
		f=1;//f先置为1
		for(j=0;j<b.length();j++)//遍历b字符串
		{
			if(a[i+j]!=b[j])
			{
				f=0;
				break;
			}
		}
		if(f==1)
		{
			cout<<i<<endl;
			return 0;
		}
	} 

	cout<<-1<<endl;

	return 0;
}
小灸舞 2016-05-18
  • 打赏
  • 举报
回复

#include<iostream>
using namespace std;

void ProcessString(char str[])
{
if(*str >= 'a' && *str <= 'z')
*str = *str - 32;
}

int main()
{
char str[64];
cin >> str;
ProcessString(str);
cout << "转换后:" << endl <<str << endl;
}
赵4老师 2016-05-18
  • 打赏
  • 举报
回复
C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\strstr.c
/***
*strstr.c - search for one string inside another
*
*       Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
*       defines strstr() - search for one string inside another
*
*******************************************************************************/

#include <cruntime.h>
#include <string.h>

/***
*char *strstr(string1, string2) - search for string2 in string1
*
*Purpose:
*       finds the first occurrence of string2 in string1
*
*Entry:
*       char *string1 - string to search in
*       char *string2 - string to search for
*
*Exit:
*       returns a pointer to the first occurrence of string2 in
*       string1, or NULL if string2 does not occur in string1
*
*Uses:
*
*Exceptions:
*
*******************************************************************************/

char * __cdecl strstr (
        const char * str1,
        const char * str2
        )
{
        char *cp = (char *) str1;
        char *s1, *s2;

        if ( !*str2 )
            return((char *)str1);

        while (*cp)
        {
                s1 = cp;
                s2 = (char *) str2;

                while ( *s1 && *s2 && !(*s1-*s2) )
                        s1++, s2++;

                if (!*s2)
                        return(cp);

                cp++;
        }

        return(NULL);

}

3,882

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧