Thursday, July 9, 2009

C++ Help!!?

I need to write a program that skips blanks in the input and prints the input text with no blanks at all.





So, for the input: Once there was a tree.....


The output would be: Oncetherewasatree.....





The program should skip ALL the white spaces including tabs, if they appear in the input line.





Please help me, all I know is to create a program that counts the white spaces rather than deletes them, please I am in desperate need of help!!!!

C++ Help!!?
I just woke up, so any real code I wrote would be garbage. In metacode, though... and very Java looking meta (sorry!)





for i=0 to inputString.length {


.. while inputString.char(i) is not " " or null {


.... append inputString.char(i) to outputString


.... }


.. }


stdout %26lt;%26lt; outputString








Should grab anything that's not a space or end-of-line and push it to your output which you would send to the screen or wherever you need it. You'd also want to check that the string was not empty before you work through it.





p.s. Y!Answers definitely needs to handle code formatting better... at least leading spaces!





Rob's answer below is what I was aiming for!
Reply:I am not all that familar with C++ syntax so I'll just think this through.





1. Put the input into an Array


2. Iterate through each element of the array.


3. Create a new String for the output.


4. If the element is not equal to ' ' (blank) add the character to the output string variable.


5. Display the output.





Hope that at least can get you started. Until someone more familiar with C++ syntax can help out...
Reply:Hows about something like ...





%26lt;code%26gt;


char * mystring = "Once there was a tree...";


unsigned int len = strlen(mystring)


for(int i = 0; i %26lt; len; i++)


... if (NULL == strchr("\t\r\n ", mystring[i]))


... ... cout %26lt;%26lt; mystring[i];


%26lt;/code%26gt;





that way you just loop through each character, see if it is one of those white space characters using strchr and if not (NULL result) then you output that character.





Note: I haven't tested the above code. Your milage with it may vary. ;)


No comments:

Post a Comment