字符串中的子字符串,不使用任何标准API [英] substring from string without using any standard api
本文介绍了字符串中的子字符串,不使用任何标准API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,
我写下以下逻辑来查找字符串中的子字符串位置但我的逻辑失败。如果子字符串是还有一个地方。
Hello,
I wrote below logic to find substring position in string but my logic is failed.If sub-string is there more then one place.
#include "stdafx.h"
#include<stdio.h>
#include<string.h>
#include<iostream.h>
int compareposition(char* str1,char str2);
int main()
{
char SensexGraphUrl[100];
memset(SensexGraphUrl,0,100);
int i,j;
char* strarray="SENSEXGRAPHURL";
char* str1= "SE";
int count=0;
int iLen = strlen(strarray);
int iLen1 = strlen(str1) ;
int pos=0;
for(i=0;i<iLen1;i++)
{
pos = compareposition(strarray,str1[i]);
cout<<"Pos"<<pos <<endl;
//strncpy((char*)strarray[pos],"",1);
if(iLen1 == 2)
{
pos = compareposition(strarray,str1[i]);
cout<<"Pos"<<pos <<endl;
}
}
return 0;
}
int compareposition(char* str1,char str2)
{
int j;
int istrlen = strlen(str1);
for(j=0;j<istrlen;j++)
{
if(str1[j] == str2)
return j;
else
continue;
}
return 0;
}
。
.
推荐答案
for(i=0;i<ilen1;i++)>
{
pos = compareposition(strarray,str1[i]);
如果我是4,
会崩溃因为str1只有两个字符和一个终止零。
使用调试器,就像所有程序员一样; - )
should crash if i is 4 because str1 has only two chars and a terminating zero.
use a debugger as all programmers do ;-)
你逻辑中的一个缺陷:
第一个应该循环搜索 ed 字符串SENSE ...,而不是搜索字符串SE。然后,对所有后续caracters进行一对一比较,如果不匹配则进行中断。
There is a flaw in your logic:
The first for should loop on the searched string "SENSE...", not on the search string "SE". Then, do a one to one comparison for all following caracters, and interrupt if no match.
这篇关于字符串中的子字符串,不使用任何标准API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文