有关字符串比较的问题

lily604 2008-03-05 06:55:09
我定义了 char* ch="daceg";
bool bl[10];
ifstream in("trade.txt");
getline(in,s);
想从文件一行一行读入字符,然后和ch进行比较
如果一行中有d则把bl[0]=1;否则,bool[0]=0;
知道把daceg全部比较完形成一个数组,比如10111,或11011之类的 ,请大家帮看看怎么做好
用 strncmp 该怎么编啊
...全文
61 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
lily604 2008-03-05
  • 打赏
  • 举报
回复
我用了第一位的代码,因为比较好懂,所以 20 ,10喽
薛勇 2008-03-05
  • 打赏
  • 举报
回复
兄弟,给你举个例子,
int a=strcmp(b,"daceg")
cout<<a<<endl;
假如说返回值a=-1,则可以判断出b<daceg
a=0,可以判断出b和daceg相等,也就是说b就是daceg
a=-1,可以判断出b>daceg。
然后就是一些字符串的操作,自己做吧
Chappell 2008-03-05
  • 打赏
  • 举报
回复


#include <stdio.h>
#include <stdlib.h>

#include <string.h>

void main( void )
{
char buffer[100];
char* pp="abcdf";
int nlen = strlen(pp);
int* bl = new int[nlen];
FILE *stream;

if( (stream = fopen( "test.txt", "r" )) == NULL )
exit( 1 );

/* Cycle until end of file reached: */
while( !feof( stream ) )
{
/* Attempt to read in 10 bytes: */
if(fgets(buffer,100, stream)==NULL)
{
printf( "fgets Error" );
break;
}
if( ferror( stream ) )
{
printf( "Read error" );
break;
}
char* ptem = pp;
int i=0;
while(buffer[i]!=0 && ptem[i]!=0 && buffer[i]!='\n')
{
if(buffer[i]==ptem[i])
{
bl[i]=1;
}
else
{
bl[i]=0;
}

printf("%d ",bl[i]);
i++;
}
printf("\n");
}
fclose( stream );
delete []bl;
bl = 0;
}
星羽 2008-03-05
  • 打赏
  • 举报
回复


#include "iostream"
#include "fstream"
#include "string"
using namespace std;

int main()
{
const char* ch = "daceg";
bool bl[10];

ifstream in("trade.txt");

while (true)
{
string s;
getline(in, s);

if (in.fail())
break;

for (size_t i = 0; i < strlen(ch); ++i)
{
if (s.find(ch[i]) != -1)
bl[i] = true;
else
bl[i] = false;
cout<<bl[i];
}
cout<<endl;
}


return 0;
}

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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