![]() |
|
#1
|
|||
|
|||
need help on c-stringsI'm working on a problem where the user inputs the first name, middle name and last name and should produce the output, last name, first name then midde initial.
example: input: John Smith Doe output: Doe, John S. the program should also work even if the user does not put the middle name. example: input: John Doe output: Doe, John So far my program takes care only of the first example and I can't figure out on how to take care of the second example. So far here is what I got. CPP / C++ / C Code:
|
|
#2
|
||||
|
||||
Re: need help on c-stringsHi fidel,
Welcome to GIDForums. ![]() The second example is somewhat complicated. Dont worry.We'll discuss how to do it. We do not know whether the user will enter three names or not. So, the better option is get the entire line the user enters. This can be done using getline function. CPP / C++ / C Code:
The next step is to calculate how many strings are there in the line. If there is only two strings, we understand that the user has not entered the middle name. If there is three strings, the user has entered all of his names. We are using an array of strings instead of separate names. this helps in easy coding. CPP / C++ / C Code:
CPP / C++ / C Code:
Consider that 'j' is a variable that indicate the current name. If j is 0, then name[j] indicates the first name. if j is 1, name[j] indicates the second name, and if j is 2, name[j] indicates the third name. So, initialize j to zero at the start. Next, how to find out that how many strings are there in the line? Here comes the problem. We should check character by character in string line. For example, consider that the user enters this: Code:
So,
CPP / C++ / C Code:
Then, in the end, check whether j is 0, 1 or 2. If j is 1, the user has only entered his first name. So, print name[0]. else if j is 2, the user has entered first and last names only. So, print name[0] and name[1]. else, j is 3. So the print name[0], name[1] and name[2]. Note that this is a simple program, that works only if the user enters three strings with only one space between them. Like this: Code:
Suppose that the user enters like this: Code:
Now, the program fails! Because we have not checked for multiple spaces inbetween the words. What can we do now? We should increment j only once in multiple spaces that occurs in line. So, we should increment j only if: 1. present character line[i] is a space, and 2. previous character should not_be_equal to a space. Regards, Paramesh. __________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. |
|
#3
|
|||
|
|||
Re: need help on c-stringsthanks a lot Paramesh
|
|
#4
|
|||
|
|||
Re: need help on c-stringsim still having problems. somehow it would not display the desired results
CPP / C++ / C Code:
|
|
#5
|
||||
|
||||
Re: need help on c-stringsHi fidel,
You cant do like this: (in the for loop) CPP / C++ / C Code:
And since you want to use j later in the program, you can declare them at the start itself. Like this: CPP / C++ / C Code:
Then, in the end, check whether j is 0, 1 or 2. And should use double quotes when you are checking conditions. Like this: CPP / C++ / C Code:
With that change, the first hurdle is over.! Regards, Paramesh. __________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. |
Recent GIDBlog
Python ebook by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Strings tripping me up once more | Elsydeon | C++ Forum | 5 | 04-Dec-2005 17:41 |
| need help - questions about strings | Benayoun | C Programming Language | 6 | 24-Jan-2005 02:15 |
| array of pointers to strings | mirizar | C++ Forum | 5 | 21-Jan-2005 10:24 |
| C++ style strings and STL | dexter | C++ Forum | 14 | 04-Jan-2005 07:46 |
| I am reviewing Arrays and need help converting some strings to arrays | jenmaz | C Programming Language | 22 | 22-Nov-2004 23:26 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The