Look at last post.
Look at last post.
Last edited by daywalker-; 10-31-2007 at 07:01 PM.
First, you have to come up with an algorithm on how you will you do this. Will you read in each word one at a time? Read in one character? Read in an entire line? How can you identify which characters need capitalization (or need it removed)? How can you handle a space before punctuation, since you have to check the next character to see whether the space should be output.
While it's not the best idea, I'll be honest and say that I recently worked on something very similar and ended up diving into coding and coming up with the algorithm as I went. It was easier for me to visualize as I got a little code down. You can always do it that way, too.
To start coding, begin with an empty main. Then add the code to output the initial prompt to cout. Compile it and run it to make sure it works. That's your code start. Then start implementing your algorithm, compiling and checking the output along the way.
For example, maybe you want to use cin.get() in a loop to read one character at a time. You can start by adding the code to break the loop when a period is read in. Compile that and test it and make sure it stops on the period.
Keep doing these steps and pretty soon you will have aworking program. Feel free to post any specific problems you find along the way.
I use a like 3-4 for loops, to get rid of extra spaces and leading spaces, and for only the first letter being capitalized and the I's. How can i include all these under 1 for loop, with ifs?
like
Code:int strshiftleft(string,str & int 0) { for(int i=0, i < (str.length() ); i++) { if( (i+1) != str.length() ) { str[i]=str[i+1]; } str.resize(str.length()-1);
Could u print out some code that could do that?
-Capitalize the first letter.. while the rest are lowercase
-no space in between
-no leading space so " It" ---> "It"
-and no spacing before a punctuation "Hi ." ---> "Hi."
I have no idea for these.. really..
please help
That's what you have to figure out. Use toupper and tolower to change the capitalization of a single character.
To remove a space, you need to identify the spot where it shouldn't be and remove it from your string or just skip it from the output.
You have to use logic to figure it out, that's what the assignment is testing.
I would read character by character (like the assignment recommended) and keep a couple of variables to remember what the previously read characters are.
If you know what a finite state machine is, I would write one for this, it will help a lot.
This assignment is a bit over for my whole class, no one is understanding.
Can someone tell me how i can revise the functions in this????
Code:void correctionspace(string& sent, int offset)//function for space { for(int i = offset; i < sent.length(); i++) { sent[i]=sent[i+1]; } sent.resize( sent.length() - 1 );// putting string to the left } int main() { int i; string sent = ""; char temp; cout << "Type in a sentence:"; do { temp = cin.get(); sent = sent + temp; }while(temp != '.');// sta int ispunct(temp) ; cin.sync(); while (isspace(sent[0])) { correctionspace(sent, 0); } for(i=1;i<int(sent.length());i++) { sent[i]=tolower(sent[i]); } for(i=1;i<int(sent.length());i++) { if( isspace(sent[i]) && isspace(sent[i+1]) ) { correctionspace(sent, i); i = 0; } } for(i=0;i<(sent.length());i++) { if( isspace(sent[i]) && (sent[i+1] == '.') ) { sent.erase(i,1); i = 0; } } sent[0]=toupper(sent[0]); cout<< "The correct punctuation of what has been entered is:" <<sent<<endl; return 0; }
> This assignment is a bit over for my whole class, no one is understanding.
Well then the whole class needs to take it up with the teacher right now, because it isn't going to get any easier.
> Can someone tell me how i can revise the functions in this?
Revise how?
You need to tell us things like
- what error messages you get (if any)
- what input you provide and output you get, and how that differs from what you expect.
Show us that you're doing more than just dumping your code on the board and hoping.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
You really need to study the string class and it's member functions. There is quite a bit that can help you out there if you know it exists. For instance:
There is a function called getline that will help here:Code:string sent = ""; char temp; cout << "Type in a sentence:"; do { temp = cin.get(); sent = sent + temp; }while(temp != '.');
Code:string sent; cout << "Type in a sentence:"; getline(cin,sent,'.'); // Read everything up till the first '.' char sent += '.'; // Add the '.' back that was removed from the above getline call
Don't know what you're trying to accomplish here, but should be removed from the code.Code:int ispunct(temp) ; cin.sync();
Ok, so you're trying to basically remove any leading spaces right? The string container has a find_first_not_of member function that is useful here. Find the first non-space character in the string and if it isn't the first character of the string (index 0) then you've got leading space that needs to be removed. In that case, call the substr (or erase) function with the index/value returned from the find_first_not_of function:Code:while (isspace(sent[0])) { correctionspace(sent, 0); }
This avoids the repeated and wasteful looping. Essentially this does a single "shift" no matter how many leading space chars there are while your code does a shift of the whole string for every single leading space.Code:std::string::size_type indx = sent.find_first_not_of(" "); // Find first non-space char if( indx > 0 ) { sent = sent.substr(indx); // Reset sent to start from the first non-space //sent.erase(0,indx); // Also a possibility }
Something similar can be done with removing the trailing spaces from the string (those prior to the last '.' character). There is a find_last_not_of member function that you could combine with a call to the erase (or substr) member function that's handy for that. Things become especially easy if you don't add back that end '.' (as I did in the getline example) until the very end (you know it's there logically but you don't have to physically add it back until later) so you don't have to work around it.
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens