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. >>

Re: Convert first character from lowercase to uppercase? 80 80

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'
*/
Re: Convert first character from lowercase to uppercase? 80 80

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!!!!

Re: Convert first character from lowercase to uppercase? 80 80

>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);
}
Re: Convert first character from lowercase to uppercase? 80 80

>*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. ;)

Re: Convert first character from lowercase to uppercase? 80 80

>*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

Re: Convert first character from lowercase to uppercase? 80 80

>*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.

Re: Convert first character from lowercase to uppercase? 80 80

>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 );
Re: Convert first character from lowercase to uppercase? 80 80

>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.

Re: Convert first character from lowercase to uppercase? 80 80
void covertToUppercase(char title);

This is different from...

void covertToUppercase(char *title);
Re: Convert first character from lowercase to uppercase? 80 80
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.

Re: Convert first character from lowercase to uppercase? 80 80

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

covertToUppercase(title);
Re: Convert first character from lowercase to uppercase? 80 80

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

Re: Convert first character from lowercase to uppercase? 80 80

>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;
  }
}
Re: Convert first character from lowercase to uppercase? 80 80

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 );
}
Re: Convert first character from lowercase to uppercase? 80 80

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

Re: Convert first character from lowercase to uppercase? 80 80

>how do i fix that?
It was written for C++. Include <cctype> instead of <ctype.h> 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.

Re: Convert first character from lowercase to uppercase? 80 80

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;
  }
}
Re: Convert first character from lowercase to uppercase? 80 80

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.

Re: Convert first character from lowercase to uppercase? 80 80

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.