toupper

I've written a program to produce random sentences. I'm having trouble getting the first letter of the first word into uppercase. Can anyone help?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#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 word1;
	
	word1 = rand() % 5;

   	sentence[0] =  article[word1] ;

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

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

	
}

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

   	sentence[1] =  noun[word1] ;

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

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

   	sentence[2] =  verb[word1] ;

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

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

   	sentence[3] =  preposition[word1] ;

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

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

   	sentence[4] =  article[word1] ;

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


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

   	sentence[5] =  noun[word1] ;

	printf("%s ", sentence[5]);
}
I am guessing that toupper() is from one of the classes you included. I don't know what to do if your bent on using a function from a class, but the way I do that sort of thing is fairly simple.

1
2
3
4
5
toUpper(char *word){ /* with a func name like this i would have assumed it would
                        capitalise the whole word, but i make this to cap the first letter */
  if(word == NULL) return;
  if(word[0] >= 'a' && word[0] <= 'z') word[0] += (char)('A' - 'a');
}


I don't know what problem you are having, but you could try changing your arrays to type int. I know that in stdio.h, strings are not char* but int*, because of how EOF works.
Last edited on
toupper() takes a char and returns a char, you are passing it a char*
The correct syntax is

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

sentence[0] is a pointer to the first member of the char* array
sentence[0][0] is the first char of the first member of the char *array

Normally, if sentence was something like char sentence[40], then your syntax would have been correct, but in this instance you have char* sentence[6], so you have to remember to deal with the array first, hence sentence[0][0]

Hope that helps.
Last edited on
Topic archived. No new replies allowed.