Computer Science Canada comparing strings and characters |
Author: | weijie [ 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; } |
Author: | jcollins1991 [ 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
and use the same bracketing throughout your code... Right now your code isn't even readable... |
Author: | Homer_simpson [ 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/ |