![]() |
|
#1
|
|||
|
|||
C++ student needs helps ASAPI have been working on these programs for three days straight now. I have searched all over the internet for help but I can't seem to find any. I have the three programs written but I'm not getting the answers I'm supposed to be getting.
The first assignment is: Write a C++ program that accepts a string from the user and then replaces all occurrences of the letter e with the letter x. And here is what I've come up with... CPP / C++ / C Code:
The program runs, but when I enter a sentence, nothing changes. The second assignment is: a. Write a function named countlets ( ) that returns the number of letters in a string passed as an argument. Digits, spaces, punctuation, tabs and newline characters should not be included in the returned count. b. Include the countlets ( ) method written for Exercise 6a in an executable C++ program and use the program to test the method. And here is what I've come up with: CPP / C++ / C Code:
I am not sure where to use countlets () and the result I get when I run the program is: The original string is: 32 characters, which consist of 1 letters The third assignment is: Write a C++ program that accepts a string from the console and displays the string one word per line. I have no idea how to approach this. I wrote what I thought was correct but I got a bunch of errors. This is what I came up with: CPP / C++ / C Code:
Any help would be appreciated. I'm really trying to understand this programming. If anyone can give me a step by step on what I'm doing wrong and explain my errors it would help a lot. Thanks in advance. |
|||
|
#2
|
||||
|
||||
Re: C++ student needs helps ASAPFor your first program, you're using replace incorrectly. The replace function replaces a string for a set of characters from a specified index and continuing for a specified length. Confused? I'll try an example.
CPP / C++ / C Code:
So basically, you specify where to begin the replacement (the first argument), and the next n letters (the second argument) are replaced with a string (the third argument). Since this only works for one segment at a time, you'll need a loop. CPP / C++ / C Code:
So you need to take out the declaration of e and x (not sure what you were trying to accomplish with those anyway) and put the above loop in place of your replace statement. In your second program, the structure is way off. You never end your for loop, and as a result you put two brackets at the end of the program (I assume make the compiler happy). Your program should look like this: CPP / C++ / C Code:
The for loop could be simplified to CPP / C++ / C Code:
For the third program, use the string replace function again. However, this time, replace ' ' with "\n". This will force a new line between each word. Hope this helps! __________________
Gamer_2k4 |
|
#3
|
|||
|
|||
Re: C++ student needs helps ASAPQuote:
1. The length of the new string is made equal to the whatever the value of the int variable e is. 2. All chars in the string are given a value of whatever the value of char(x) is. Since the values of the variables e and x are undefined in your program, the string is undefined. Since you don't try to do anything with the new string, maybe the program won't crash (or, maybe it could --- I really don't know), but one thing I would bet on: it won't do anything useful to your assignment. Why not simply step through your message and see if each char is equal to 'e'? If it is, then replace it with 'x', otherwise, leave it alone. CPP / C++ / C Code:
Regards, Dave |
|
#4
|
|||
|
|||
Re: C++ student needs helps ASAPI completed the first problem (the e, x program). I go the second problem to work and here's what I've come up with:
CPP / C++ / C Code:
It works but I'm not sure what to do with countlets ( ) ....any suggestions? |
|
#5
|
|||
|
|||
Re: C++ student needs helps ASAPMake a new function countlets that takes a strung and then use that for loop
|
|
#6
|
|||
|
|||
Re: C++ student needs helps ASAPQuote:
Try something like this ( note I didn't change any actual code, just rearranged it so that you are using a separate function, instead of having it all in main() ): CPP / C++ / C Code:
Personally, I like this as a first pass on the function countlets(): CPP / C++ / C Code:
less ... messy. |
|
#7
|
|||
|
|||
Re: C++ student needs helps ASAPQuote:
I just ran what you posted and it compiled with a bunch of errors. |
|
#8
|
|||
|
|||
Re: C++ student needs helps ASAPQuote:
I dunno, it compiles and runs for me.... ( the version you quoted was just before I edited it to fix a typing mistake..... try it with what's there now...) |
|
#9
|
|||
|
|||
Re: C++ student needs helps ASAPI dunno..I run it and get this:
CPP / C++ / C Code:
I'm probably the one messing something up. |
|
#10
|
|||
|
|||
Re: C++ student needs helps ASAPQuote:
Yeah, in the function "countlets" where it says "str" change it to "s" CPP / C++ / C Code:
|
Recent GIDBlog
Ph.D. progress by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| C Code i need help for printing | batman3280 | C Programming Language | 8 | 13-Mar-2006 14:48 |
| Help in C Print is not working with LinkList | batman3280 | C Programming Language | 3 | 09-Mar-2006 19:03 |
| Problem with program | breggo | C++ Forum | 3 | 08-Jun-2005 13:51 |
| MultiDimensional Array from a .dat file | joeyz | C Programming Language | 2 | 05-May-2005 19:26 |
| Operator overloading (not happening) | gmn | C++ Forum | 11 | 30-Aug-2003 08:18 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The