Arman Iqbal
Arman Iqbal

Reputation: 755

How do I create a function that alters a string?

I have created this program as homework for school. The program takes a first and a last name seperated by space, and then preceeds to capitalize the first character of the first and last name and changes the rest of the letters to lower case.

I have succeeded in doing this, but I want to do the altering of letters inside a function. I have created lots of functions before but never with strings involved. As I have come to understand you have to use pointers for this purpose but I do not understand in what way really.

My program code is as follows:

//The program only accepts first and last name seperated by 1 space.
#define MAX 31


int main()
{
    char name[MAX];
    int i;

    printf("What is your name? (First name and last name only): ");
    fgets(name, sizeof(name), stdin);
    name[strlen(name)-1] = 0;


    for (i=0; i<strlen(name); i++)
    {
        if (i==0)
            name[i]=toupper(name[i]);

        else if(name[i] == ' ') {
            name[i+1]=toupper(name[i+1]);
            i++;
        }

        else
            name[i]=tolower(name[i]);
    }

    printf("Name = %s\n", name);

    return 0;

}

Basically I want to have the entire for loop inside a function. I would appreciate any help on the subject. I would also be very thankful for any links or websites that might provide me with more knowledge on the subject.

Thanks in advance.

Upvotes: 0

Views: 82

Answers (2)

sadakatsu
sadakatsu

Reputation: 1273

In C and C++, an array variable (such as your name) is actually a pointer. Arrays are a memory space in your program's stack that are large enough to fit the required number of elements in a row (such as MAX ints). The array variable is a pointer that refers to the memory address of the first element of the array, and using the [] operator performs pointer arithmetic under the hood to get the desired element. That's why the first element of an array is indexed as 0: name[0] is equivalent to *(name + 0) or just *name.

Like mah said in his comment, you can create a function that receives an array variable as its argument that has your for loop as its body.

That being said, when you are passing arrays to functions, it is almost always necessary to pass a second argument that refers to the size of the array (or at least the number of elements). Pointers refer to only a single memory address, and it is only through careful use that they can be used to represent arrays. Otherwise, you can end up with exceeding the array's bounds and either crash your program (good result) or corrupting other memory (bad result).

With C-style strings, the convention is to have the null character as the last character, so strlen() counts characters until it finds it. This might be a safe instance to not pass the array's size.

Upvotes: 0

Filipe Gon&#231;alves
Filipe Gon&#231;alves

Reputation: 21233

There's not much to it:

void myfunc(char name[]) {
    int i;
    for (i=0; i<strlen(name); i++)
    {
        if (i==0)
            name[i]=toupper(name[i]);

        else if(name[i] == ' ') {
            name[i+1]=toupper(name[i+1]);
            i++;
        }

        else
            name[i]=tolower(name[i]);
    }
}

And in main():

myfunc(name);

In this case you don't even have to think about pointers. In fact, char name[] in the function declaration will be treated as char *name, and name in myfunc(name) will decay into a pointer to the first character of your string. But since strings are null-terminated, and you use strlen(), the code stays exactly the same.

Oh, and a quick note: you should take that strlen(name) out of the loop and do it before. Calling strlen() on every iteration to test for the loop condition is unnecessary and inneficient.

Upvotes: 3

Related Questions