I am currently taking a class in C online and I have a homework assignment to create a random sentence generator with arrays of specific words. I have the random part working, but I am unsure as to how to capitalize the first letter of the first word in the sentence. I could create a separate array with the capitalized words, but I don't think that conforms to the problem. This is what I have so far, and like I said it works except for the capital letter. I have searched my textbook and online and can't find a way to do this, but I'm pretty sure it is possible. I don't want the answer just pasted here, I want to understand how to do it so an explanation would also be appreciated. Thanks.

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

int main(void)
{

 const char *article[ 5 ] = {"the", "a", "one", "some", "any" }; 
 const char *noun[ 5 ] = { "boy", "girl", "dog", "town", "car" };
 const char *verb[ 5 ] = { "drove", "jumped", "ran", "walked", "skipped" };
 const char *preposition [ 5 ] = { "to", "from", "over", "under", "on" };
 const char *sentence[ 35 ] = {0};
 int a=0, b=0, c=0, d=0, e=0, f=0;
 int counter=0;

 srand(time(NULL));

 for (counter=1; counter <= 20; counter++)
 {
     a = rand() % 5;   /*random numbers for each array of words */
     b = rand() % 5;
     c = rand() % 5;
     d = rand() % 5;
     e = rand() % 5;
      f = rand() % 5;
       
  sentence[1] = article[a];
  sentence[2] = noun[b];
  sentence[3] = verb[c];
  sentence[4] = preposition[d];
  sentence[5] = article[e];
  sentence[6] = noun[f];
   

      printf("%s %s %s %s %s %s.\n", sentence[1],sentence[2],sentence[3],sentence[4],sentence[5],sentence[6]);
    

     }

printf("\n");

system("PAUSE");
return 0;
}

if i have a character array, say: char myCharString[16]; and i want to capitalize the first letter (ie, the first element of the array), then I use the function "toupper" found in the <string.h> library, and it will do exactly what i want it to do: myCharString[0] = toupper(myCharString[0]); however... if your exercise prevents you from using <string.h> libaray functions, then you will need to convert it by hand.

first recall the ASCII code values of characters, and that a lowercase letter is exactly 32 (0x20) greater than its uppercase version, as designated by ASCII code. So to convert the lowercase 'a' (0x61 in ASCII) to 'A' (0x41) you would subtract 0x20 from the former (or 32 decimal, if you prefer), like so: myCharString[0] -= 0x20; however, doing this by hand has its dangers. you will have to FIRST CONFIRM that the character is indeed both a lowercase letter (and not anything otherwise), else the result of subtracting 0x20 from some other character can easily crash your program.


EDIT: One thing to remember the type char is, for all intents and purposes, an 8-bit int. char and int can be operated upon in the same way with the only exception being that int is generally 32 bits.


.

I can use the toupper command, but I'm having trouble capitalizing only the first letter of the word. I'm able to print the capitalized character alongside the original word, but I'm at a loss for how to edit the sentence array with the capitalized word.

EDIT:

Basically I have the sentence array which contains 6 random words. I need to capitalize the first letter of the first word which I believe is at sentence[1][0], but I am not sure how to store the information to this location so that I can call it with printf.

toupper returns the value of the character that is passed in as an argument, after it is operated upon.

value1 = toupper('a');
value2 = toupper('Z');
value3 = toupper('\n');

value1 is the only one that is different than the input. its value is 'A'. value2 and value3 are the same as the input ('Z' and '\n') since they are not uppercase characters.

so whatever the value returned is, just assign it back into the place from which you pulled the original character (the first letter)

consider the code fragment example:

char capitalizeMe[10] = "lowercase";

capitalizeMe[0]=toupper(capitalizeMe[0]);
capitalizeMe[5]=toupper(capitalizeMe[5]);

the result is that the word 'lowercase' is transformed to 'LowerCase'


.

my mistake: "toupper" is in the <ctype.h> library, not <string.h>

I guess what I'm not understanding is how to point toupper to the first letter of the first word. I attempted to place this before the assignment of sentence[1] article[a][0] = toupper(article[a][0]); , but my program just crashes. This is the first I've worked with multi-dimensional arrays, so I'm guessing I'm just missing something simple.

The problem probably lies in how you use indexes. Note that array indexes are zero-based i.e. if you have an array holding 5 elements, the last valid index is 4, not 5. Consider

const char *article[ 5 ] = {"the", "a", "one", "some", "any" }; 
int i;
// print all 5 strings .. valid indexes are 0 .. 4
for(i = 0; i < 5; ++i)
{
    printf("%d=%s\n", i, article[ i ]);
}

With this regard you need to revise your code.

Thanks to everyone that replied, I ended up solving it by printing the first word as individual characters instead of trying to replace the entry in the array. Not sure that its the most efficient way to do it, but it works and gets my homework done.

well, actually, that's a terrible way to do it.

i guess partial credit is better than no credit, but i still dont understand why you didnt assign the output of "toupper" back into the array element from which you got it, in the first place?

what part of this didnt we explain?

I understand the concept, but applying it to the program just wouldn't work. The code I posted a few posts back just crashed my program, though if I put the same thing into a printf command it will print the single character. I can't get the program to store the capitalized word in the array. When I think I have it, the program crashes or doesn't print it with a capital letter.

The problem stems from the fact that instead of an array of letters making up a word, I'm working with an array of words making up a sentence. I can't break it down to capitalize only one character.

i just noticed you have defined your "sentence" array as pointer to "const char"... the "const" means that you can't change it. you'll want to define it as type "char"

a separate issue is that you have it as a pointer... if you're going to copy a string into it (and you will need to), then you need to lose the pointer and declare it as an array of strings. char sentence[7][35]; (i dont think you need 35 characters per word, since each of the "sentence" arrays is just a copied word... but thats a trivial point.

now you'll have to use "strcpy" to copy the word into the "sentence" array... you cant use assignment operators (= signs) to copy strings.

sorry i didnt see this earlier. :(


.

since you tried so hard, and i ran you around in circles, here's the fix:

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

int main(void)
{

const char *article[ 5 ] = {"the", "a", "one", "some", "any" }; 
const char *noun[ 5 ] = { "boy", "girl", "dog", "town", "car" };
const char *verb[ 5 ] = { "drove", "jumped", "ran", "walked", "skipped" };
const char *preposition [ 5 ] = { "to", "from", "over", "under", "on" };
char sentence[7][35] = {0};
int a=0, b=0, c=0, d=0, e=0, f=0;
int counter=0;

srand(time(NULL));

for (counter=1; counter <= 20; counter++)
{
    a = rand() % 5;   /*random numbers for each array of words */
    b = rand() % 5;
    c = rand() % 5;
    d = rand() % 5;
    e = rand() % 5;
    f = rand() % 5;
      
    strcpy(sentence[1], article[a]);
    strcpy(sentence[2], noun[b]);
    strcpy(sentence[3], verb[c]);
    strcpy(sentence[4], preposition[d]);
    strcpy(sentence[5], article[e]);
    strcpy(sentence[6], noun[f]);
    
    sentence[1][0] = toupper(sentence[1][0]);

    printf("%s %s %s %s %s %s.\n", sentence[1],
	sentence[2],sentence[3],sentence[4],
	sentence[5],sentence[6]);
}

printf("\n");

system("PAUSE");
return 0;
}

Ok, I think I understand it now.

I modified my code and it works now, just one quick note. strcpy is in <string.h>, so that needs added as well.

Thanks again.

yes, you're quite right. thanks for noting that, it will probably help someone looking at this later :)


.

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.