1

I've learned Python in the past and I am very confused with how C handles strings.

My approach for the pset2 "initials" program was to check for every character next to a space and add it to a string. However I am having a lot of trouble understanding how to add a char to a string.

This is how I get the first initial. No problems up until here.

// first initial
char c = toupper(name[0]);

Getting the rest of the initials:

// rest of initials
string initials;
for (int i=0; i<strlen(name); i++) {

    if (isspace(name[i])) {

        char v = name[i+1];
        // add v to initials

    }

}

What can I do once I have v in order to add it to the initials string that my program is going to output at the end? Or is my approach completely wrong?

edit: I just figured out a much better approach to this problem. But at least this was a good way to learn about arrays :)

2 Answers 2

2

usually you do only what you need to do. no more and no less. here, you don't really need to store the initials into a string and then print that whole string out (you could though). all you need to do is to print them out and you could do that directly.

another thing is that you don't need your loop to start at i = 0 since you already took care of name[0] before the loop.

but just to answer your question

how to add a char to a string?

you should first know what a string is. in C, a string is basically a char array whose last element is a '\0'. as you may know, array sizes in C are fixed — once you create an array of a particular size, you can't change that whether by extending it or reducing it (1).

so as long as you have room in your char array, you can easily add a char to that string. for example:

char str[5] = "car"; /* there's an empty slot we used 4 chars 5 
                        {'c', 'a', 'r', '\0'}*/

str[3] = 'd'; // append 'd'
str[4] = '\0'; // terminate the string; str is now "card"

you could do a similar job using the strcat function from the standard library, but technically you'd be concatenating a single-char string to another not a char to a string. execute man strcat for more on that.

if you don't know the exact size before hand, you're typically gonna need to specify a maximum size that you presumably know.

if the size depends on a variable, you can create a variable-length array (allowed in ISO C99+). keep in mind though that an array size specified using a variable doesn't mean that the array size will change as the value in the variable change. the rule is still held — once the size of an array of specified, it's fixed. for example:

unsigned int size = 10;
char str[size]; // str is of size 10
size = 20;
// str is still of size 10

(1) technically, you can sort of get around that using some dynamic memory allocation techniques (addressed later in the course).

3
  • Thank you for the very helpful answer! Sadly the compiler is rejecting my declaring a variable length array even though it is using c99 standard. Do you know what I might be doing wrong?
    – mlamp
    Commented Jun 19, 2015 at 22:20
  • @otmeek what's the error message?
    – kzidane
    Commented Jun 19, 2015 at 22:22
  • nevermind, I was initialising it. That was the error message it was throwing out. I didn't realise you couldn't initialise variable length arrays. It all makes sense now, thanks again!
    – mlamp
    Commented Jun 19, 2015 at 22:26
-1

The std::string type has a + operator defined that accepts characters (as well as other strings), so you can simply use something like initials += name[i + 1] to achieve your goal.

2
  • in C, there is no std::string and the + operator doesn't concatenate chars to strings.
    – kzidane
    Commented Jun 19, 2015 at 21:38
  • Oh, right, sorry about that, my mistake. I'm a full-time software develper working with C++ and it gets me confused sometimes. :)
    – skreborn
    Commented Jun 20, 2015 at 6:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .