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 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 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 typechar 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.
.
jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
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'
.
jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
my mistake: "toupper" is in the library, not
jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
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.
mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
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?
jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
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. :(
.
jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
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;
}
jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
yes, you're quite right. thanks for noting that, it will probably help someone looking at this later :)
.
jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179