2

I have solved the initials program in 2 different ways. In the third way, there is a bug which I am not able to clear, please help me finding it out.

code:

#include<stdio.h>
#include<ctype.h>
#include<cs50.h>
#include<string.h>


string make_Initials(int len_Name, string full_Name);   // this function takes the length of the name and the name itself entered by the user.

int main(void)
{
    string user_Name = GetString();    // stores the name entered by the user
    int len = strlen(user_Name);       // stores the lenght of the name entered by the user.



    string print_Initials = make_Initials(len, user_Name);  // stores the string returned by the make_Initials function.

    printf("%s\n", print_Initials);
    return 0;
}

/* this function takes the length of the name and the name itself entered by the user. This returns the string containing
** initials in UPPERCASE. */    
string make_Initials(int len_Name, string full_Name)
{
    char result_Initials[30];        // stores each letter immediately after the space between the words in the user's name.

    result_Initials[0] = toupper(full_Name[0]); // storing the 1st letter of the name into the 1st position of result_Initials.

    int j = 0;
    for(int i=0; i<len_Name; i++)
    {
        if(!(isalpha(full_Name[i])))     /* this for loop checks each character for space, if space is found then
                                         ** the letter immediately after the space in full_Name is stored into result_Initials*/
        {
            ++j;
            printf("\n");
            result_Initials[j] = toupper(full_Name[i+1]);
        }

    }

    string final_Result = result_Initials;       //storing the content of result_Initials array into a string variable, then returning the string variable.

    return(final_Result);
}

I know I have made a simple problem complex. I just wanted to implement the userdefined functions concept in this program thats why the complexity.

the bug is: when I use the printf function in the for loop in the function make_Initials, I get a correct output

sadfs jlj

SJ

when I comment off the printf function in the for loop in the function, I dont get an output at all.

sdsdf luiuewrn

/home/ubuntu/workspace/myPsets/ $ 

I am a beginner, Please point out the problem in detail. Thank you.

0
2

The main problem (and probably just a misunderstanding due to lack of further knowledge) is the distinction between a char array char[], and a char pointer char*, which is typedefed as string.

Your make_Initials() function returns a char * (aka string as you will learn later on) but instead you assign to final_Result a char array (char[]) and return that, which is not the same.

Also you haven't terminated your string with a null zero \0. Those two changes should fix your code.

Change

char result_Initials[30];

to

string result_Initials = malloc(30 * sizeof(char));  // allocate 30 chars for the string

and right after the for loop, add

result_Initials[j + 1] = '\0';  // end the string with a null zero

and also right before the return 0 in your main() add this

free(print_Initials);  // free the allocated memory

If you don't understand some of the above changes, you are welcome to ask questions but rest assured, you will learn all about them in the next lessons.

extra tip:

In your make_Initials() function, instead of checking for

if(!(isalpha(full_Name[i])))

you can use

if(full_Name[i] == ' ')

since you have to check only for the space character. This will be faster (not noticeable though) and more elegant.

Hope this answers your question and fixes your program. :)

3
  • I spent almost half an hour using the debugger trying to figure out why it doesn't work. I knew it had to do with null termination of the string, but I couldn't make the connection to the fact that it was declared with a char array. Suffice it to say I'm not too proficient in the art of pointers, malloc, and other such nonsense. That being said, why does it somehow work when that lil prinf in the for is uncommented? Aug 1 '16 at 20:01
  • No one can know that, since the previous code's behavior is undefined, which means it can lead to uncertain results. You may execute it 1000 times in a row and not get the same result every time.
    – ChrisG
    Aug 1 '16 at 20:07
  • I suppose that the same result couldn't be guaranteed for 1000 times with code of this nature, but it seemed to work fine every time I ran the code which was at least a dozen times. Something's purty fishy here. Aug 1 '16 at 20:13

You must log in to answer this question.

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