string display error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rabbitysun
    New Member
    • Feb 2013
    • 3

    string display error

    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
    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.
    
    
    }
    Last edited by Banfa; Feb 20 '13, 09:37 AM. Reason: You can link out to images like that you need to attach the image to the post or just copy and past the text of the command prompt.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    At line 40 you make every string 512 characters long. the C++ string class does not use '\0' as a terminator (necessarily) and so your output strings are actually all 512 bytes long, it looks like '\0' is then being output as a space.

    If you took advantage of the find_first_of and find_first_not of members of string then you could probably make your algorithm simpler.

    If you use a vector to output the results instead of an allocated array you could make the split in a single pass of the string instead of 2 passes.

    If you used the features of string, like push_back, you would not have to initialise the output strings at all.

    Comment

    Working...