I've written a program to produce a random sentence. I'm having trouble converting the first letter of the first word into uppercase. Don't know where to go from here. Can anyone help?

#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <ctype.h>

void firstword( char *article[5], char *sentence[6]);
void secondword(char *noun[5], char *sentence[6]);
void thirdword(char *verb[5], char *sentence[6]);
void fourthword( char *preposition[5], char *sentence[6]);
void fifthword( char *article[5], char *sentence[6]);
void sixthword(char *noun[5], char *sentence[6]);

int main()
{

	char *article[5]= {"the", "a", "one", "some", "any"};
	char *noun[5] = { "boy", "girl", "dog", "town", "car"};
	char *verb[5]= {"drove", "jumped", "ran", "walked", "skipped"};
	char *preposition [5] ={ "to", "from", "over", "under", "on"};
	char *sentence [6];


	srand(time(0));

	firstword( article,sentence);
	secondword(noun, sentence);
	thirdword(verb, sentence);
	fourthword(preposition, sentence);
	fifthword(article, sentence);
	sixthword(noun, sentence);

	printf("\n");

	
		
		return 0;
}

void firstword( char *article[5], char *sentence[6])
{

	char word;

	word = rand() % 5;

	sentence[0] =  article[word] ;

	sentence[0] = toupper(sentence[0]);
	printf("%s ", sentence[0]);

	
}

void secondword(char *noun[5], char *sentence[6])
{
	char word;
	
	word = rand() % 5;

   	sentence[1] =  noun[word] ;

	printf("%s ", sentence[1]);
}

void thirdword(char *verb[5], char *sentence[6])
{
	char word;
	
	word = rand() % 5;

   	sentence[2] =  verb[word] ;

	printf("%s ", sentence[2]);
}

void fourthword( char *preposition[5], char *sentence[6])
{
	char word;
	
	word = rand() % 5;

   	sentence[3] =  preposition[word] ;

	printf("%s ", sentence[3]);
}

void fifthword( char *article[5], char *sentence[6])
{
	char word;
	
	word = rand() % 5;

   	sentence[4] =  article[word] ;

	printf("%s ", sentence[4]);
}


void sixthword(char *noun[5], char *sentence[6])
{
	char word;
	
	word = rand() % 5;

   	sentence[5] =  noun[word] ;

	printf("%s.", sentence[5]);
}

Recommended Answers

All 3 Replies

sentence[0] = toupper(sentence[0]);

sentence[0] is a char array, but toupper() only excepts 1 char as input.
This is a C program, but you've posted in the C++ forum. Are you allowed to use C++? Because std::string would make your live a whole lot easier.

Niek

Member Avatar for iamthwee

Maybe:

void firstword ( char *article[5], char *sentence[6] )
{
   char word;
   word = rand() % 5;

   sentence[0] = article[word];

   int s_t = strlen ( sentence[0] );
   for ( int i = 0; i < s_t; i++ )
   {
      if ( i == 0 )
      {
         putchar ( toupper ( sentence[0][0] ) );
      }
      else
         printf ( "%c", sentence[0][i] );
   }

}

Thanks for the help. I'll post on the right forum next time :$ I think i've worked out my own way. It may be the long way around the problem but its working!

sentence[0] =  article[word] ;

	if(sentence[0] == "the"){
		printf("The ");
	}
	if(sentence[0] == "a"){
		printf("A ");
	}
	if(sentence[0] == "any"){
		printf("Any ");
	}
	if(sentence[0] == "some"){
		printf("Some ");
	}
	if(sentence[0] == "a"){
		printf("A ");
	}
Be a part of the DaniWeb community

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