Cavalkaf: here it is: write a program that searches how many times, and where it it, a string that the user inputs Cavalkaf: so if the user inputs hi Cavalkaf: and the second word is hi Cavalkaf: it should say "The word hi is the 2nd word in the file." BigDog6700: wait, does the user enter in two strings? BigDog6700: one to search for and one to search? Cavalkaf: no Cavalkaf: one of them is a file BigDog6700: ah BigDog6700: okay Cavalkaf: a "search" feature BigDog6700: mmmmkay BigDog6700: well what it should do is this: Cavalkaf: my initial aproach was to get word by word BigDog6700: read a search string, deliniated by commas BigDog6700: 1st word, 2nd word, 3rd word Cavalkaf: what about lines Cavalkaf: ? BigDog6700: okay Cavalkaf: and spaces BigDog6700: hold on Cavalkaf: but I get the idea BigDog6700: let me finish explaining before you ask questions BigDog6700: okay? BigDog6700: OKAY? Cavalkaf: ok Cavalkaf: :D BigDog6700: so BigDog6700: the user enters in a search string, which will be severl different strings, delineated by commas Cavalkaf: right BigDog6700: don't worry about words, if they want to search for a specific word they can enter it instead of a string BigDog6700: next BigDog6700: it should take the search string, and splice it Cavalkaf: ok..... BigDog6700: String klcka[100]; int curstring = 0; do { if(instring(searchstring, ",") != -1) { klcka[curstring] = sub(searchstring, 0, instring(searchstring, ",")); searchstring = sub(searchstring, instring(searchstring, ","), length(searchstring) - instring(searchstring, ",")); } else if(length(searchstring) >= 1) { klcka[curstring] = searchstring; searchstring = ""; } else { klcka = "error"; } curstring++; } while(length(searchstring) >= 1); //while the search string contains a comma... if(klcka[curstring -1] == "error") { cout << "Please enter a search string"; return; } etc... Cavalkaf: ok Cavalkaf: so what does sub does? Cavalkaf: and instring? BigDog6700: I'm not sure what the names for those functions are in C++ BigDog6700: hold on Cavalkaf: ok Cavalkaf: anything yet? Cavalkaf: the approach I was taking before I realized the comma problem was to get every word to a temporary string, and compare it Cavalkaf: (using cin >>) Cavalkaf: however, when I realized this comma problem Cavalkaf: I started getting this characted by character, making a string out of it until it hit a non-standart character BigDog6700: string::npos is what find returns if it doesn't find the character Cavalkaf: instring does....? BigDog6700: String klcka[100]; int curstring = 0; do { if(searchstring.find(",") != string::npos) //If there is a comma in the string { klcka[curstring] = searchstring.substr(0, instring(searchstring, ",")); //store the first string into the array searchstring = searchstring.substr(searchstring.find(","), searchstring.length() - searchstring.find(",")); // lop off the string you just removed } else if(!searchstring.empty()) // If there are no commas and a search string remains... { klcka[curstring] = searchstring; // add the string to the array searchstring = ""; // empty the string } else // If it's empty { klcka[curstring] = "error"; // error } curstring++; } while(!searchstring.empty()); //while there is still a string if(klcka[curstring -1] == "error") { cout << "Please enter a search string"; return; } BigDog6700: replace "instring" with find BigDog6700: substr(x,y) makes the string from x, y characters example: s = "kit is cool"; s = s.substr(0, 7) + "stupid"; would make s == "kit is stupid"; BigDog6700: string::npos is what find returns if it doesn't find the character Cavalkaf: ok Cavalkaf: however, I wanted something that would detect any non-stadart character Cavalkaf: yours only does commas Cavalkaf: anyway, what does instring does Cavalkaf: wow BigDog6700: ? Cavalkaf: how did a bunch of stuff showed up in 1 second? Cavalkaf: your past 3 msgs BigDog6700: ummmm BigDog6700: I keep IMming you, but your connection keeps dropping Cavalkaf: ???? Cavalkaf: it dropped 2 times Cavalkaf: not sure why..... BigDog6700: neither do I, but I know the first time was me, and the second time was you Cavalkaf: ok Cavalkaf: :-P BigDog6700: but read through the new code BigDog6700: I explained a lot Cavalkaf: I just did Cavalkaf: I got what you did BigDog6700: kk Cavalkaf: however, I still need something that will get a string from the file and remove all non a-z characters from it BigDog6700: okay Cavalkaf: also, I wanted a better way to count the words in a file Cavalkaf: so it does not count two spaces as two words Cavalkaf: or a line with no characters as a word Cavalkaf: if the string has \0 (zero) on it, it is empty, right? Cavalkaf: any ideas? BigDog6700: yeah BigDog6700: hold on BigDog6700: I made an app like this last year BigDog6700: I have it in my folder somewhere... Cavalkaf: :-D BigDog6700: ispunct(char x) isspace(char x) in ctype.h Cavalkaf: ? Cavalkaf: how is the syntax? BigDog6700: you just #include <cype.h> then use if(isspace(char)||ispunct(char)) { spacecount++; } Cavalkaf: ok Cavalkaf: I see BigDog6700: but that is a C header BigDog6700: not C++ Cavalkaf: same in C++ Cavalkaf: just checked BigDog6700: :-D BigDog6700: I is smart, no? Cavalkaf: http://www.cplusplus.com/doc/ansi/hfiles.html Cavalkaf: yes |
Maybe urs does something else, i didnt bother to read ur whole IM conversation(given that i have 15 minutes be4 i have to leave for school), but it seems to me that theres a HELL of a lot easier way to do this
Posted by: Ben at February 2, 2005 06:09 AMOh yeah, almost certainly. This was just the best I could come up with given Felipe's rather crappy description and using year old C++ knowledge.
Posted by: Kit at February 2, 2005 06:39 AMLike a StringTokenizer. Not really too positive weather or not C++ has string tokenizers, but that's an example of what could take the place of that whole while loop.
Posted by: kit at February 3, 2005 06:50 AM