Convert first character from lowercase to uppercase?

Reply

Join Date: Sep 2004
Posts: 18
Reputation: wangstarr is an unknown quantity at this point 
Solved Threads: 0
wangstarr wangstarr is offline Offline
Newbie Poster

Convert first character from lowercase to uppercase?

 
-1
  #1
Nov 8th, 2004
Hi, I am stuck and my head is spinning with this project that I am working on for my beginners c++ class. Can someone show me how to work this? Here is the problem:

Write a function that uses an array parameter to accept a string as its argument. It should convert the first letter of each word in the string to uppercase. If any of the letters are already uppercase, they should be left alone. (see hint). Demonstrate the function in a simple program that asks the user to input a string, passes it to a function, and then displays the string after it has been modified.

Here is the work I have so far: How do I just convert the first letter while keeping the rest lowercase or if uppercase leave it alone? also how do I make the numbers display as numbers? Thank you.

#include <iostream.h>

int main()
{

char ch;


cout << "Please enter a word: ";
cin >> ch;

if (ch >= 'A' && ch <= 'Z')
cout >> ch;
else if (ch >= 'a' && ch <= 'z')
cout >> toupper(char ch);
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,541
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Convert first character from lowercase to uppercase?

 
0
  #2
Nov 8th, 2004
Walk along the string, but keep a boolean variable that's true if the last character was whitespace and false otherwise. Whenever you reach a non-whitespace character and the boolean variable is true, change the character to upper case. You don't need to worry about testing if it's already upper case because toupper does the right thing regardless of the character you give it.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 18
Reputation: wangstarr is an unknown quantity at this point 
Solved Threads: 0
wangstarr wangstarr is offline Offline
Newbie Poster

Re: Convert first character from lowercase to uppercase?

 
0
  #3
Nov 8th, 2004
I don't quite understand. Sorry , I'm pretty new at this... I worked on some more coding.. is this what you mean?


bool is_upper ( char ch )
{
static char upper[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

for ( int i = 0; upper[i] != '\0'; i++ ) {
if ( ch == upper[i] )
return true;
}

return false;
}

bool is_lower ( char ch )
{
static char lower[] = "abcdefghijklmnopqrstuvwxyz";

for ( int i = 0; lower[i] != '\0'; i++ ) {
if ( ch == lower[i] )
return true;
}

return false;
}
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 18
Reputation: wangstarr is an unknown quantity at this point 
Solved Threads: 0
wangstarr wangstarr is offline Offline
Newbie Poster

Re: Convert first character from lowercase to uppercase?

 
0
  #4
Nov 8th, 2004
can someone help please?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 185
Reputation: Stack Overflow is an unknown quantity at this point 
Solved Threads: 4
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline Offline
C Programmer

Re: Convert first character from lowercase to uppercase?

 
0
  #5
Nov 9th, 2004
Greetings,

Thinking on the same line, lets walk along the string. Better yet, lets use a while loop to do so. By creating a seperate function to modify the string would probably be beneficial in this case. If we send the string to another function, we can treat it like a pointer allowing us to take the advantage of using the indirection or dereferencing operator *.

Pointers are not hard to understand, and I have written two tutorials on them. You can find them here:

Pointers (Part I): An Introduction
Pointers (Part II): Pointers In Application

Back on topic, we can create a function called checkString() if we wanted to. We will need to somehow retreive our string so lets make checkString() take one argument.

Let's look at what we can try:
#include <iostream>
#include <ctype.h>
using namespace std;

void checkString(char *str) {
	char lower[] = "abcdefghijklmnopqrstuvwxyz";

	while (*str) {
		// If not in the alphabet, increment and start again
		if (!isalpha(*str)) {
			str++;
			continue;
		}

		// If any lower letter was found in *str
		if (strchr(lower, *str)) {
			// Change to upper
			*str = toupper(*str);
		}

		// Increment location
		str++;
	}
}
Whoa! Lots to digest. Alright, lets start at the beginning. We take a single argument called str.
After this we create a local variable called lower. This contains all of our letters in the alphabet that are lower case.
Next we check if the current location of our pointer is a letter in the alphabet, though if not the loop continues and increments our array by one. If a letter is found using strchr() then we send it to upper case. The strchr() trick is quite neat. We know that strchr() checks for a letter in a string. Our letter is *str and our string is lower.
Next we increment the location of str. That is the only way to make the loop continue, as our while loop just checks if it exists and is not NULL.

Edit: Forgot to add that isalpha() is a ctype function. That is why we include ctype.h
All this does is tells us if the current location of our string is apart of the alphabet or not.

Just for an example, we could call on main() as the following:
int main (void) {
	char sentence[25] = "Hello There";

	checkString(sentence);

	cout << sentence << endl;

	return 0;
}
If you have further questions, please feel free to ask.


- Stack Overflow
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.

IRC
Channel: irc.daniweb.com
Room: #c, #shell
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,541
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Convert first character from lowercase to uppercase?

 
0
  #6
Nov 9th, 2004
>I don't quite understand.
What a surprise. Have you people even taken a problem solving course? A boolean variable is bool, it means true or false. Walk the string character by chraracter, you can do this with an int that counts from 0 to size - 1. Save whether the last character is a space. Then it's just a matter of an if statement. If this is too difficult for you then get your book out and start reading, because I really can't help you more than making the solution freaking obvious and as easy as possible.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC