473,396 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <string>
  4. using namespace std;
  5.  
  6.  
  7. std::string* DocumentLexAndNorm(std::string& document, unsigned int& numberOfWords)
  8. {
  9.     unsigned int numWordsOutput=0;
  10.     int counter=0;
  11.     for (int i=0; i<document.size(); i++)
  12.     {    
  13.  
  14.         if (isalpha(document[i]))
  15.         {
  16.             if (counter>0)
  17.                 continue;
  18.             if (counter==0)
  19.                 {numWordsOutput++;
  20.             counter++;}
  21.         }
  22.  
  23.         switch (document[i])
  24.         {
  25.         case ' ':
  26.         case '\t':
  27.         case ',':
  28.         case '.':
  29.         case ';':
  30.         case ':':
  31.         case '\'':
  32.         case '\"':
  33.         case '?':
  34.             counter=0;
  35.         }
  36.     }
  37.  
  38.     std::string* retVal = new std::string[numWordsOutput];
  39.     for (unsigned int i=0; i<numWordsOutput; i++)
  40.         retVal[i]= std::string(512, '\0');
  41.     int newNumWordsOutput=0;
  42.         int newCounter=0;
  43.  
  44.     for (int i= 0, x=0,z=0; (i<numWordsOutput) &&(x<document.size()); x++)
  45.     {    
  46.  
  47.  
  48.  
  49. string temp;
  50.  
  51.  
  52.  
  53.  
  54. temp = "";
  55.  
  56.  
  57.         if (isalpha(document[x]))
  58.         {
  59.  
  60.             if (newCounter>0)
  61.             {
  62.                     if ((document[x]<=90)&&(document[x]>=65))
  63.                         {retVal[i][z]+=document[x]+32;
  64.  
  65.                     z++;}
  66.                     else {retVal[i][z]+=document[x];
  67.  
  68.                     z++;}
  69.                     continue;
  70.             }
  71.  
  72.             if (newCounter==0)
  73.             {    
  74.                 newNumWordsOutput++;
  75.                 newCounter++;
  76.                 if ((document[x]<=90)&&(document[x]>=65))
  77.                 {retVal[i][z]+=document[x]+32;z++;}
  78.                 else {retVal[i][z]+=document[x];z++;
  79.                 }
  80.  
  81.             }
  82.  
  83.         }
  84.  
  85.         switch (document[x])
  86.         {
  87.         case ' ':
  88.         case '\t':
  89.         case ',':
  90.         case '.':
  91.         case ';':
  92.         case ':':
  93.         case '\'':
  94.         case '\"':
  95.         case '?':
  96.             {newCounter=0;
  97.             z=0;
  98.             if (isalpha(document[x-1]))
  99.                 i++;
  100.             break;}
  101.         }
  102.  
  103.  
  104.  
  105.     }
  106.     numberOfWords = numWordsOutput;
  107.     return retVal;
  108. }
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116. int main()
  117. {
  118.     //string n1[]={ "teacher", "teacher", "Teacher2"};
  119.     //string n2[]={ "teacher11", "teacher1", "STUDNET22"};
  120.     //unsigned int q=3;
  121.     //unsigned int p[]={2,3,4};
  122.     //cout << transformCriteria(p,n1,n2,q)<< endl; cout << n2[2]<<endl;
  123.     std::string document = "I'm sad that my 2 brand-new Mercedes S550s were stolen on March 13, 2012!!";
  124. unsigned int nb_words = 0;
  125. std::string* doc = DocumentLexAndNorm(document, nb_words);
  126. for (unsigned int i = 0; i < nb_words; ++i)
  127.    std::cout << "word #" << i << ": " << doc[i];
  128. std::cout << std::endl;
  129. // Free the array created at the end of the program.
  130.  
  131.  
  132. }
  133.  
  134.  
Feb 20 '13 #1
1 1283
Banfa
9,065 Expert Mod 8TB
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.
Feb 20 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Garth17 | last post by:
This totally surprised me. I'm using IE 6. I built a really simple test.aspx page that had a javascript block in it like this: <script language=javascript src="spanish.js"></script> Inside...
8
by: Dinesh Jain | last post by:
Hi al,, I have written a code which displays directory listing of files from FTP server directory.I display all the files in a listview with its associated icons. To display icons, I get the...
2
by: c676228 | last post by:
Hi, I have several user controls like email, phone,ssn etc. each has it's own validation message in its user control already. When I bring all those user controls in an aspx page, I would like to...
2
by: seberino | last post by:
Why can't I import gtk (pygtk) module? It appears gtk module want X perms? >>> import gtk Traceback (most recent call last): File "<stdin>", line 1, in ? File...
7
basstradamus
by: basstradamus | last post by:
Hi When I am trying to eveluate object literal It causing Unterminated string constans error (firefox). var obiekt = (eval("("+data+")")); the data is carring object literal recived from server...
2
by: rajuk | last post by:
Hi i have following code,when i execute this code i got unterminated string constant error.any javascript guru can you look into this please. raju /* The link details */ var links = new Array...
1
by: VUNETdotUS | last post by:
Let's say I have a string: div.innerHTML = "<a onclick='foo(\""+myWord+"\");'></a>"; in IE only (tested version 7) if var myWord = "English" then it works fine but if var myWord = "Modifier...
2
by: kummu4help | last post by:
Hi, i have a following form. <form id="frm_signup"> <b>please fill the following registration form.All the fields are compulsory</b><br/><br/> <table border="0" cellpadding=""...
1
renji1981
by: renji1981 | last post by:
Hi pals Hers my link Primus Infotech Solutions - IT Services , Product Engineering , Web Based Applications Here Im getting a Unterminated String constant Error When you click on...
0
by: Helena Tran | last post by:
I'm doing a dropdown-menu, and combine JS. It's display well in all browser, but it has an error when displaying in IE 7,8 browser: Menu dropdown in normal : ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.