i wrote the code to set the string to be 512 ending with '\0'.
when i tried to cout the string , it goes like this Image on Google Docs
what can i do to remove the huge spaces?
this is the code
when i tried to cout the string , it goes like this Image on Google Docs
what can i do to remove the huge spaces?
this is the code
Code:
#include <iostream> #include <algorithm> #include <string> using namespace std; std::string* DocumentLexAndNorm(std::string& document, unsigned int& numberOfWords) { unsigned int numWordsOutput=0; int counter=0; for (int i=0; i<document.size(); i++) { if (isalpha(document[i])) { if (counter>0) continue; if (counter==0) {numWordsOutput++; counter++;} } switch (document[i]) { case ' ': case '\t': case ',': case '.': case ';': case ':': case '\'': case '\"': case '?': counter=0; } } std::string* retVal = new std::string[numWordsOutput]; for (unsigned int i=0; i<numWordsOutput; i++) retVal[i]= std::string(512, '\0'); int newNumWordsOutput=0; int newCounter=0; for (int i= 0, x=0,z=0; (i<numWordsOutput) &&(x<document.size()); x++) { string temp; temp = ""; if (isalpha(document[x])) { if (newCounter>0) { if ((document[x]<=90)&&(document[x]>=65)) {retVal[i][z]+=document[x]+32; z++;} else {retVal[i][z]+=document[x]; z++;} continue; } if (newCounter==0) { newNumWordsOutput++; newCounter++; if ((document[x]<=90)&&(document[x]>=65)) {retVal[i][z]+=document[x]+32;z++;} else {retVal[i][z]+=document[x];z++; } } } switch (document[x]) { case ' ': case '\t': case ',': case '.': case ';': case ':': case '\'': case '\"': case '?': {newCounter=0; z=0; if (isalpha(document[x-1])) i++; break;} } } numberOfWords = numWordsOutput; return retVal; } int main() { //string n1[]={ "teacher", "teacher", "Teacher2"}; //string n2[]={ "teacher11", "teacher1", "STUDNET22"}; //unsigned int q=3; //unsigned int p[]={2,3,4}; //cout << transformCriteria(p,n1,n2,q)<< endl; cout << n2[2]<<endl; std::string document = "I'm sad that my 2 brand-new Mercedes S550s were stolen on March 13, 2012!!"; unsigned int nb_words = 0; std::string* doc = DocumentLexAndNorm(document, nb_words); for (unsigned int i = 0; i < nb_words; ++i) std::cout << "word #" << i << ": " << doc[i]; std::cout << std::endl; // Free the array created at the end of the program. }
Comment