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.

Dani AI

Generated

Good instincts from : the first character of each C-string is at index [i][0]. In C++, string literals are const, so declare the array as const char* to avoid undefined behavior.

Example that builds "MTW" and terminates it properly:

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

char initials[3 + 1];               // 3 letters + '\0'
for (size_t i = 0; i < 3; ++i) {
    initials[i] = threeDays[i][0];  // copy one char, not a string
}
initials[3] = '\0';                 // NUL-terminate

// use initials as a C-string, e.g. puts(initials);  // prints MTW

Why your strcpy(str, threeDays[0][0]) failed: strcpy expects a const char* (pointer to a NUL-terminated string), but threeDays[0][0] is a single char. To copy one character, use assignment as above. Likewise, strcat concatenates whole strings; it is not for single characters.

If you want this to scale with the array size and handle possible empty strings safely:

const char* threeDays[] = {"Monday","Tuesday","Wednesday"};
const size_t n = sizeof threeDays / sizeof threeDays[0];

char initials[n + 1];
for (size_t i = 0; i < n; ++i) {
    char c = threeDays[i] && threeDays[i][0] ? threeDays[i][0] : '?';
    initials[i] = c;                // avoid embedding '\0' mid-string
}
initials[n] = '\0';

Common pitfalls:

  • Forgetting the final '\0'.
  • Declaring string literals as char* instead of const char*.
  • Writing past the end of the destination array. Size it to count + 1.

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.