Suppose I have something like

char *threeDays[3] = {"Monday","Tuesday","Wednesday"};

and I wanted to extract the first letter of each string and create another string using those letters. How would I go about doing it? I tried

char str[4];
strcpy(str,*(threeDays+1));
strcat(str,*(threeDays+2));

but that gives me "TuesdayWednesday". I just want a string with the first letters ("MTW"). Can anyone help?

Note: I cannot use the string class. I have to use c-strings.

Recommended Answers

All 5 Replies

threeDays[0][0] = 'M'
threeDays[0][1] = 'o'
threeDays[0][2] = 'n'
threeDays[1][0] = 'T'
threeDays[2][0] = 'W'

When I try, for example,

strcpy(str,threeDays[0][0])

I get an error saying "invalid conversion from 'const char' to 'const char*'; initializing argument 2 of `char* strcpy(char*, const char*)' "... :(

When I try, for example,

strcpy(str,threeDays[0][0])

I get an error saying "invalid conversion from 'const char' to 'const char*'; initializing argument 2 of `char* strcpy(char*, const char*)' "... :(

That's because it is very wrong. See my other post for a clue.

Additional:
Just copy the characters into the character array, adding \0 as the last character.

Remember, in C as 'string' is simply an array of characters ending with \0. It's not a special thing, it's not magic. It's just an array of characters by another name.

Thanks so much for the help! I got my program to work. :)

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.