954,170 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Convert first character from lowercase to uppercase?

Hmm...how do i go about doing a conversion only the first letter of the string?

<< Thread split from [thread=13727]original[/thread] since this is taking on a life of its own. >>

Quickslvr
Newbie Poster
5 posts since Apr 2005
Reputation Points: 10
Solved Threads: 0
 

First figure out how to tell the first character of a word.

#include <stdio.h>
#include <ctype.h>

int main(void)
{
   char *ch, text[] = "The quick brown fox jumps over the lazy dog.";
   int word = 0;
   for ( ch = text; *ch; ++ch )
   {
      if ( isalpha(*ch) )
      {
         if ( !word )
         {
            word = 1;
            printf("The start of a new word is '%c'\n", *ch);
         }
      }
      else
      {
         word = 0;
      }
   }
   return 0;
}

/* my output
The start of a new word is 'T'
The start of a new word is 'q'
The start of a new word is 'b'
The start of a new word is 'f'
The start of a new word is 'j'
The start of a new word is 'o'
The start of a new word is 't'
The start of a new word is 'l'
The start of a new word is 'd'
*/
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

Hi,

I have declared a string:

char title[MAXTITLE] = "the Empire Strikes Back";


and a function to convert the first letter of the string 't' to 'T'

void convertToUpperCase(char *sPtr)
{
      while(*sPtr != '\0')
      {
         if (islower(*sPtr))
              *sPtr = toupper(*sPtr);
       }
}


<< moderator edit: added [code][/code] tags >>

Is not working tho...can someone help me on this problem? Thank you!!!!

Quickslvr
Newbie Poster
5 posts since Apr 2005
Reputation Points: 10
Solved Threads: 0
 

>and a function to convert the first letter of the string 't' to 'T'

No. You have a function to convert the entire string to uppercase. (That is, it would if you would increment the pointer so the loop would eventually terminate.) If you are not intending to loop, you function becomes even more trivial.

void convertToUpperCase(char *sPtr)
{
   *sPtr = toupper(*sPtr);
}
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

>*sPtr = toupper(*sPtr);

*sPtr = toupper ( (unsigned char)*sPtr );

Since you can't be sure that *sPtr was a character returned by a standard input function. Better safe than sorry in these situations. ;)

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

>*sPtr = toupper(*sPtr);

*sPtr = toupper ( (unsigned char)*sPtr );

Since you can't be sure that *sPtr was a character returned by a standard input function. Better safe than sorry in these situations. ;)

Since I had to refresh my memory, I'll let everyone else in on it too. http://www.stanford.edu/~blp/writings/clc/ctype-cast.html

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

>*sPtr = toupper(*sPtr);

*sPtr = toupper ( (unsigned char)*sPtr );

Since you can't be sure that *sPtr was a character returned by a standard input function. Better safe than sorry in these situations. ;)

I have tried this function, it did convert the letter 't' to 'T' however the output prints 'T' only and not the string. When i do a for loop yes it prints the string but every word becomes a capital letter.

Quickslvr
Newbie Poster
5 posts since Apr 2005
Reputation Points: 10
Solved Threads: 0
 

>When i do a for loop yes it prints the string but every word becomes a capital letter.
If you only want the first letter to be capitalized, only call the function on the first character:

char s[] = "this is a test";

s[0] = toupper ( s[0] );
printf ( "%s\n", s );
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

>When i do a for loop yes it prints the string but every word becomes a capital letter. If you only want the first letter to be capitalized, only call the function on the first character:

char s[] = "this is a test";

s[0] = toupper ( s[0] );
printf ( "%s\n", s );
#include <iostream>
#include <cctype>
#define MAXTITLE 128

using namespace std;

void PrintCharacters(const char *);
void covertToUppercase(char title);

int main()
{
	char title[MAXTITLE] = "the Empire Strikes Back";
	
	//cout << "Enter title: ";
	//cin >> title;

	cout << "Before converting to Upper Case: ";
	PrintCharacters(title);
	cout << endl;

	cout << "After converting to Upper Case: ";
	covertToUppercase(title);
	cout << endl;
	
return 0;
}

void PrintCharacters(const char * sPtr)
{
	for(; *sPtr != '\0'; sPtr++)
		cout << *sPtr;
}

void covertToUppercase(char title)
{
	title[0] = toupper(title[0]);
	cout << title;

	/*
	for (; *sPtr !='\0'; sPtr++)
	{
		*sPtr = toupper ((unsigned char) *sPtr);	
			cout << *sPtr;	
	}
	*/
	/*
	while (*sPtr != '\0')
	{
		if (islower(*sPtr))
			*sPtr = toupper((unsigned char)*sPtr);
			cout << *sPtr;
	sPtr++;
	}
	*/
}
titles.cpp: In function `int main()':
titles.cpp:22: error: invalid conversion from `char*' to `char'
titles.cpp:22: error:   initializing argument 1 of `void covertToUppercase(char)'
titles.cpp: In function `void covertToUppercase(char)':
titles.cpp:36: error: invalid types `char[int]' for array subscript
titles.cpp:36: error: invalid types `char[int]' for array subscript


<< moderator edit: added [code][/code] tags >>

Sorry to bother you guys again, can you kindly explain where have i gone wrong? I know it will be easier if i could use string libraries but i ain't allowed to do so. Can you kindly help me out on this? Thank you.

Quickslvr
Newbie Poster
5 posts since Apr 2005
Reputation Points: 10
Solved Threads: 0
 
void covertToUppercase(char title);


This is different from...

void covertToUppercase(char *title);
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 
void covertToUppercase(char title);

This is different from...

void covertToUppercase(char *title);

Yes, i know it is different one is a pointer and one is not. Hmm..however i didn't use pointer in converUpperCase function.

Quickslvr
Newbie Poster
5 posts since Apr 2005
Reputation Points: 10
Solved Threads: 0
 

You're passing it a pointer, as the error message states.

covertToUppercase(title);
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

what if i want to convert the first letter of each word in the string to uppercase?
help please, thanks

aismm
Newbie Poster
10 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

>what if i want to convert the first letter of each word in the string to uppercase?
You convert the first character and the first character after whitespace to upper case:

void first_word ( char *s )
{
  bool upper = true;

  while ( *s != '\0' ) {
    if ( upper && !std::isspace ( (unsigned char)*s ) ) {
      *s = std::toupper ( (unsigned char)*s );
      upper = false;
    }
    else if ( !upper && std::isspace ( (unsigned char)*s ) )
      upper = true;

    *++s;
  }
}
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

how about something like this:

#include "stdio.h"
#include "string.h"
#include "ctype.h"
int main( int argc, char * argv[] )
{
		char original[1025];
		char proper[1025];
		memset( original, '\0', 1025 );
		memset( proper, '\0', 1025 );
		int length=0;
		if ( argc < 2 )
		{
				printf( "USAGE: %s string_to_do_proper_case_on\n", argv[0] );
				return 1;
		}
		strcpy( original, argv[1] );
		length = strlen( original );
		printf( "You passed in [%s], with length of [%d]\n", original, length );
		for ( int i=0; i< length;i++ )
		{
				if ( i == 0 )
				{
						proper[i] = toupper( original[i] );
				}
				else
				{
						if ( original[i-1] == ' ' )
						{
								proper[i] = toupper( original[i] );
						}
						else
						{
								proper[i] = tolower( original[i] );
						 }
				}
		}
		printf( "Proper case string is [%s]\n", proper );
}
winbatch
Posting Pro in Training
466 posts since Feb 2005
Reputation Points: 68
Solved Threads: 18
 

thanks for the help, i used the function method
it says i have 3 errors

C:\Program Files\Microsoft Visual Studio\MyProjects\ch7case\proper.cpp(29) : error C2039: 'isspace' : is not a member of 'std'
C:\Program Files\Microsoft Visual Studio\MyProjects\ch7case\proper.cpp(31) : error C2039: 'toupper' : is not a member of 'std'
C:\Program Files\Microsoft Visual Studio\MyProjects\ch7case\proper.cpp(34) : error C2039: 'isspace' : is not a member of 'std'

how do i fix that?
thanks again

aismm
Newbie Poster
10 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

>how do i fix that?
It was written for C++. Include instead of and compile as C++. If that doesn't work, get a newer compiler. If that's not an option, just remove the std:: parts until it compiles.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

thanks for the help; there's no error now.
but perhaps im doing something wrong in main because nothing shows up converted ... it's simply blank.
what did i do wrong?

//This program converts the first letter of each word uppercase.
#include <iostream>
#include <cctype>
using namespace std;

void firstWord(char *s);

int main()
{
	const int size= 181;
	char s[size]; 
	
	cout << "Enter a line with no more than 180 characters: \n";
	cin.getline(s, size);
	cout << endl;

	cout << "You entered: \n ";
	firstWord(s);
	cout << endl;
	return 0;
}


void firstWord (char *s )
{
  bool upper = true;

  while ( *s != '\0' ) 
  {
    if ( upper && !isspace ( (unsigned char)*s ) ) 
	{
      *s = toupper ( (unsigned char)*s );
      upper = false;
    }
    else if ( !upper && isspace ( (unsigned char)*s ) )
      upper = true;

    *++s;
  }
}
aismm
Newbie Poster
10 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

You never attempt to print it out..

cout << endl;

will only put a new line onto the screen. If you want to see your value converted, you need to cout the variable s.

winbatch
Posting Pro in Training
466 posts since Feb 2005
Reputation Points: 68
Solved Threads: 18
 

YAY!!! i got it!!! im soo happpyyy :D thank you guys so much!!

aismm
Newbie Poster
10 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You