| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1045 人关注过本帖
标题:试了很多次都不行·········why
只看楼主 加入收藏
checoly
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2004-9-11
收藏
 问题点数:0 回复次数:4 
试了很多次都不行·········why

编函数int find(char s[],char t[]),该函数在字符串s中查找字符串t,如果找到,则返回字符串t在字符串s中的位置(整数值);否则返回-1。注意:用数组方式及两重循环来实现该函数。仅在find的花括号中填写若干句。

#include<iostream.h> #include<string> int find(char s[],char t[]); const int MAXLINE=256; int main() {char source[MAXLINE],target[MAXLINE]; cout<<"Please in put a string for searching:\n"; cin.getline(source,MAXLINE); cout<<"Please input a string you want to find:\n"; cin.getline(target,MAXLINE); int intPos=find(source,target); if(inPos>=0) cout<<"Find it.The target string is at index"<<inpos<<"of the source string\n"; else cout <<"Not finding it.\n"; return 0; } int find(char s[],char t[]) { }

试了很多次都不成功,又没有答案·········

帮帮忙········

呵呵·······

搜索更多相关主题的帖子: why 
2004-09-17 20:58
checoly
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2004-9-11
收藏
得分:0 

这是我最后写的····

乱七八糟····唉······

#include<iostream.h> #include<string> int find(char s[],char t[]); const int MAXLINE=256; int main() {char source[MAXLINE],target[MAXLINE]; cout<<"Please in put a string for searching:\n"; cin.getline(source,MAXLINE); cout<<"Please input a string you want to find:\n"; cin.getline(target,MAXLINE); int intPos=find(source,target); if(intPos>=0) cout<<"Find it.The target string is at index"<<intPos<<"of the source string\n"; else cout <<"Not finding it.\n"; return 0; } int find(char s[],char t[]) {int len1=strlen(s),len2=strlen(t); int i=0,count=0; char *p=t; for(int j=0;j<len2;j++) {for(;i<len1;i++) if(char s[i]==p) count++;

} p++; return (i-count+1); else return -1; }


2004-09-17 21:00
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
收藏
得分:0 

#include<iostream.h> #include<string.h>

int find(char s[],char t[]); const int MAX=15;

void main() { char source[MAX],target[MAX]; cout<<"Please in put a string for searching:\n"; cin.getline(source,MAX); cout<<"Please input a string you want to find:\n"; cin.getline(target,MAX);

cout<<source<<'\n'<<target<<'\n'<<endl;

int Pos = find(source,target);

if(Pos>-1) cout<<"Find it at "<<Pos<<" line\n"; else cout <<"Can't find it.\n"; }

int find(char s[],char t[]) { int len1=strlen(s),len2=strlen(t); int count=0;

for(int i=0;i<len1;i++) if(s[i]==*t) { count=strcmp(s+i,t); break; } if(count==0) return i; else return -1; }

为了便于调试,改了一下。

[此贴子已经被作者于2004-09-18 11:48:23编辑过]

2004-09-18 11:13
checoly
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2004-9-11
收藏
得分:0 

无论输入什么,都说找不到喔·······

而且他说用2重循环········


2004-09-18 18:23
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
收藏
得分:0 

应该是strcmp函数用得不好,我再看看先。

5.字符串比较函数strcmp 格式:strcmp(字符数组名1,字符数组名2)功能:按照ASCII码顺序比较两个数组中的字符串,并由函数返回值返回比较结果。 字符串1=字符串2,返回值=0; 字符串1>字符串2,返回值〉0; 字符串1<字符串2,返回值〈0。 本函数也可用于比较两个字符串常量,或比较数组和字符串常量。 #include"string.h" main() { int k; static char st1[15],st2[]="C Language"; printf("input a string:\n"); gets(st1); k=strcmp(st1,st2); if(k==0) printf("st1=st2\n"); if(k>0) printf("st1>st2\n"); if(k<0) printf("st1   本程序中把输入的字符串和数组st2中的串比较,比较结果返回到k中,根据k值再输出结果提示串。当输入为dbase时,由ASCII 码可知“dBASE”大于“C Language”故k〉0,输出结果“st1>st2”。

[此贴子已经被作者于2004-09-18 18:45:50编辑过]

2004-09-18 18:44
  • 5
  • 1/1页
  • 1
快速回复:试了很多次都不行·········why
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015647 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved