查找字符串,返回匹配字符串的开始位置。

maybachvip 2008-04-17 02:07:35
就比如说输入 hello后又输入 e 这时返回的是2就是位置,
代码如下,
高人告诉下哪错了?
#include <iostream>
int position(char t,char *y);
int main()
{
cout <<"请输入一串字符: (长度 <80) " <<endl;
char s[80];
cin>> s;
char insert;
cout<<"请输入一个字符:"<<endl;
cin>>insert;
int p=position(insert,s);
cout << "字符串的位置是: " <<p <<endl;
}
int position(char t,char *y)
{

for(int i=0;y[i]=!t;i++)

return i;
}
...全文
361 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
maybachvip 2008-04-17
  • 打赏
  • 举报
回复
谢谢各位了
c_spark 2008-04-17
  • 打赏
  • 举报
回复
补充一下...
#include    <iostream> 

using namespace std;

int position(char t,char *y);
int main()
{
cout <<"请输入一串字符: (长度 <80) " <<endl;
char s[80];
cin>> s;
char insert;
cout <<"请输入一个字符:" <<endl;
cin>>insert;
int p=position(insert,s);
if(p==-1)
cout << "字符串不存在此字符"<<endl;
else
cout << "字符串的位置是: " <<p <<endl;
}
int position(char t,char *y)
{
int i;
for(i=0;y[i]!=t && y[i] != '\0';i++);
if(y[i] == '\0')
return -1;
return i+1;
}
冰矿 2008-04-17
  • 打赏
  • 举报
回复

#include <iostream>
using namespace std;
int position(char t,char *y);
int main()
{
cout<<"请输入一串字符:(长度<80)"<<endl;
char s[80];
cin>>s;
char insert;
cout<<"请输入一个字符:"<<endl;
cin>>insert;
int p = position(insert,s);
cout<<"字符串的位置是:"<<p<<endl;

system("PAUSE");
return 0;
}
int position(char t,char *y)
{
int i;
for(i=0;y[i]!=t;i++); /*你把不等号写反了!!*/
return i+1; /*这里加个1*/
}

结果

请输入一串字符:(长度<80)
hello
请输入一个字符:
e
字符串的位置是:2
请按任意键继续. . .
jieao111 2008-04-17
  • 打赏
  • 举报
回复
推荐find_frist_of()
Inhibitory 2008-04-17
  • 打赏
  • 举报
回复
int position(char t,char *y)
{

for(int i=0;y[i]=!t;i++)

return i;
}
还有就是你的循环中,是不等于循环继续, 循环一继续, 程序就马上返回, hello 的第一个字符h不等于e, 所以条件为true, 循环继续, 马上返回i( i 为 0).

#include    <iostream>
using namespace std;

int position(char t, char *y);
int main() {
cout <<"请输入一串字符: (长度 <80) " <<endl;
char s[80];
cin >> s;
char insert;
cout <<"请输入一个字符:" <<endl;
cin >> insert;
int p=position(insert, s);
cout << "字符串的位置是: " << p <<endl;
}
int position(char t, char *y) {
int i = 0;
while (*y != '\0') {
if (*y == t) {
return i;
}

++i;
++y;
}

return -1;
}
c_spark 2008-04-17
  • 打赏
  • 举报
回复
lz程序太猛了啊...
#include    <iostream> 

using namespace std;

int position(char t,char *y);
int main()
{
cout <<"请输入一串字符: (长度 <80) " <<endl;
char s[80];
cin>> s;
char insert;
cout <<"请输入一个字符:" <<endl;
cin>>insert;
int p=position(insert,s);
cout << "字符串的位置是: " <<p <<endl;
}
int position(char t,char *y)
{
int i;
for(i=0;y[i]!=t;i++) ; //=! -> !=
return i+1;
}

输入:
hello
输出:
2
qiucp 2008-04-17
  • 打赏
  • 举报
回复
上面的代码还有问题,当所查找的字符在字符数组中没有的话,就会出错。修改如下:
当返回0值时,表示所查字符在字符数组中不存在。

int position(char t,char *y)
{
int i = 0;
if (y == NULL)
{
return 0;
}
while (*y != '\0')
{
i++;
if (t == *y)
{
return i;
}
y++;
}
return 0;
// for(int i=0; y[i]=!t; i++)
// return i;
}
qiucp 2008-04-17
  • 打赏
  • 举报
回复
问题出在函数 position 中。
帮你修改了下:

int position(char t,char *y)
{
int i = 0;
if (y == NULL)
{
return 0;
}
while (*y != '\0')
{
i++;
if (t == *y)
{
break;
}
y++;
}
return i;
// for(int i=0; y[i]=!t; i++)
// return i;
}
Inhibitory 2008-04-17
  • 打赏
  • 举报
回复
你的编程能编译通过?见鬼
因为你使用的是#include <iostream>
而在程序中直接使用cout,

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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