Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 comparing strings and characters
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
weijie




PostPosted: Thu Jun 03, 2010 8:00 pm   Post subject: comparing strings and characters

I need to compare strings of words (which extracted from a txt file) with local alphabet (a to z).
the problem accur when i using strcmp . anyone can help ?

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
char* alphe;
int i,j;
int alphe_flag[256]={0};
string line;

ifstream myfile ("example.txt");
if(myfile.is_open()){
while(!myfile.eof()){
getline(myfile,line);
alphe="e";
for(i=0;i<line.length();i++){
for(j=0;j<25;j++){
if(!strcmp(line[i],alphe))
alphe_flag[i]+=1;
alphe++;}}
myfile.close();
}
else cout<<"Unable to open file";
cout<<alphe_flag[5]<<endl;
return 0;
}
Sponsor
Sponsor
Sponsor
sponsor
jcollins1991




PostPosted: Thu Jun 03, 2010 8:43 pm   Post subject: Re: comparing strings and characters

Before you expect people to read your code try using the code tags

code:
like this


and use the same bracketing throughout your code... Right now your code isn't even readable...
Homer_simpson




PostPosted: Fri Jun 04, 2010 1:07 am   Post subject: Re: comparing strings and characters

change ur if statement
if(line.find(alphe)!=string::npos)

using find built in
http://www.cplusplus.com/reference/string/string/find/
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: