This assignment got me puzzled...

4 manipulations of a full name in a string using 1 function.

Page 1 of 1

8 Replies - 540 Views - Last Post: 27 February 2009 - 02:29 PM Rate Topic: -----

#1 OrangeSection  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 07-October 08

This assignment got me puzzled...

Post icon  Posted 27 February 2009 - 10:55 AM

The assignment : "Write a program that takes a string(full name) from the user and manipulates it according to these rules shown below."

* A full name is to have NO starting or finishing spaces(whitespace).

* There is to be exacly ONE space between names(like so, "Flash Dexter Gordon").

* Every new word(=part of full name) should start with an UPPER-letter, the rest shall be LOWER-letter.

* Use ONLY 1 function for the manipulation.

* The string is to be read into "main".


I read this as the name "__bAroN______voN__mUnchAusen___" should become "Baron_Von_Munchausen". ( _ = whitespace )


I had skipped this assignment because i got nowhere for 2 days, and moved on to other things, but now i want to finish it FOR GOOD....

Care to help me....? Im quite lost at this one.

hmm...going to need "tolower/toupper", "remove whitespace" ,"separate words in name" and "add whitespace?" or something like that , right?.

But how do i do it? im stuck right away here......

Heres the code i got so far, its not much but im stuck :

#include <iostream>

using namespace std;

const int SIZE = 80;
void format(char *name[SIZE]);


int main()
{
	cout << "Enter a name : ";
	char name;
	cin.getline(name, SIZE);

	
	format(&name); //error here
	cout << name;
	return 0;
}

void format(char *name[SIZE])
{
	//error here
	while(isspace(name[SIZE]) && isspace(name[SIZE+1])) // a try at finding the connected whitespaces
		{
		  int j;
		  for(j = SIZE+1; j <= strlen(name)-1; j++)
		  {
			  if(j==(strlen(name)-1))
			  {
				(*name)[j]='\0';
			  }
			  else
			  {
				(*name)[j]=name[j+1];
			  }
		  }
		}

}


This post has been edited by OrangeSection: 27 February 2009 - 10:56 AM


Is This A Good Question/Topic? 0
  • +

Replies To: This assignment got me puzzled...

#2 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: This assignment got me puzzled...

Posted 27 February 2009 - 11:28 AM

[rules][/rules]

Oh, I get it. That's why you were asking about the 80 white spaces.

This post has been edited by Hyper: 27 February 2009 - 11:29 AM

Was This Post Helpful? 0
  • +
  • -

#3 OrangeSection  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 07-October 08

Re: This assignment got me puzzled...

Posted 27 February 2009 - 11:37 AM

View PostHyper, on 27 Feb, 2009 - 10:28 AM, said:

[rules][/rules]

Oh, I get it. That's why you were asking about the 80 white spaces.

Im not asking for a solution to the whole thing... :crazy:

Im just stuck right now, not getting the *feel* for the algorithm and the whitespace-problem, i just want some advise how to move on. :D
Was This Post Helpful? 0
  • +
  • -

#4 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: This assignment got me puzzled...

Posted 27 February 2009 - 11:41 AM

Just a moment, I think I've figured out what you did wrong (in like four spots).

EDIT: This isn't the answer to your problem, but it is a huge hint of advice on how to complete it. :)
#include <iostream>
using namespace std;

void FormatName(char *Name) {

    for (int x = 0; x < strlen(Name); x++) {
        if (isspace(Name[x])) { printf("SPACE"); }
        if (isupper(Name[x])) { printf("UPPERCASE"); }
        if (islower(Name[x])) { printf("LOWERCASE"); }
        printf("%c", Name[x]);
    }

    return;
}

int main() {

    char Name[80] = {'\0'};
    cout << "Enter a name: ";
    cin.getline(Name, 80);
    FormatName(Name);

    _flushall();
    cin.get();
    return 0;
}


Welcome, hope this helps! :)

This post has been edited by Hyper: 27 February 2009 - 11:43 AM

Was This Post Helpful? 1
  • +
  • -

#5 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: This assignment got me puzzled...

Posted 27 February 2009 - 11:51 AM

You could also use strstream:

#include <iostream>
#include <strstream>

using namespace std;

void format(const char *, char *);

int main()
{
	char name[50];

	format("	BREnton	 sauNdERS	", name);

	cout << name << endl;

	return 0;
}

void format(const char *full_name, char *formatted_name)
{
	istrstream stream(full_name);
	bool first_name = true;

	strcpy(formatted_name, "");

	while(!stream.eof()) { // Any more names?
		char name[15]; // I've seen many names longer than 15 characters, though

		stream >> name;

		strlwr(name); // Yeah, this is non-standard
		name[0] = toupper(name[0]); // Make the first character uppercase

		if(!first_name) // If this isn't the first name,
			if(strlen(name) > 0) // add a space to the end of the name before it
				strcat(formatted_name, " "); // to seperate it from itself

		strcat(formatted_name, name);

		first_name = false;
	}
}

This post has been edited by bsaunders: 27 February 2009 - 11:51 AM

Was This Post Helpful? 1
  • +
  • -

#6 OrangeSection  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 07-October 08

Re: This assignment got me puzzled...

Posted 27 February 2009 - 12:01 PM

thx guys, although i have not been introduced to the fine arts of "streams" or "printf" yet in class, it got me going, those hints got me over the first hill.

I might get back to you on the rest later ;) hope not...

Thanks again...

This post has been edited by OrangeSection: 27 February 2009 - 12:02 PM

Was This Post Helpful? 0
  • +
  • -

#7 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: This assignment got me puzzled...

Posted 27 February 2009 - 12:09 PM

Oh! I'm sorry. I'm so used to coding in C I totally forgot.
cout and printf are basically the same thing.

C++ uses cout.
C uses printf.

cout << Name[x]; is what the printf should be.

Thanks for using the "This Post Was Helpful!" button, welcome for the help.
Post the program when you get it completed and working. :P

Streams? Those are apart of C++ I believe, and you'll learn about them later (they're wonderfully useful sometimes).
Was This Post Helpful? 0
  • +
  • -

#8 OrangeSection  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 07-October 08

Re: This assignment got me puzzled...

Posted 27 February 2009 - 12:26 PM

no problem, and thanks for clearing that up...:)
Was This Post Helpful? 0
  • +
  • -

#9 crazyjugglerdrummer  Icon User is offline

  • GAME OVER. NERD WINS.
  • member icon

Reputation: 119
  • View blog
  • Posts: 690
  • Joined: 07-January 09

Re: This assignment got me puzzled...

Posted 27 February 2009 - 02:29 PM

This might help: string manipulation tutorial
The main thing is extracting chars from the string, then testing them for different conditions, and manipulating them in different ways.

Hope it helps!
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1