-4

I have a problem with a code in C. I need to enter a list of names separated with the enter button. The input is stopped when the user enters the word "QUIT". the program needs to print the list of names in an alphabetical order (all letters are lowercase).

The number of names as well as the length of each name is unknown, and needs to be dynamic allocated. Also, if a name appears more than once in the input, it should appear only once in the output.

here's an example for how the code should run:

Please enter a list of names:

john

chris

ben

chris

david

QUIT

There are 4 names:

ben

chris

david

john

I thought about using a dynamic allocated array of pointers, in which each pointer contains each name and is dynamic allocated. The problem is that I don't know how to write it without getting a run-time error.

Note: at this point, I am not allowed to use things that I haven't learned yet, like structs and recursion, and can only use the libraries stdio.h, stdlib.h and string.h.

Thanks in advance.

here's the code (it's not complete, but I'm getting a runtime error at this point):

char **nameList;
int i = 0, j = 0, size = 0, check = 0;
printf("Please enter list of names:\n");
//allocate one cell of memory to the list
nameList = (char**)malloc(sizeof(char));
if (nameList == NULL)
{
    printf("Cannot allocate Memory");
    return 0;
}
//Add the first letter to the first string in the array
nameList[i][j] = getchar();
size += sizeof(char);
while (check != 1)
{
    //check if current entered letter is not an enter
    while (nameList[i][j] != '\n')
    {
        //allocated another char sized memory to the string
        nameList = (char**)realloc(nameList, (size + sizeof(char)));
        if (nameList == NULL)
        {
            printf("Cannot allocate Memory");
            return 0;
        }
        j++;
        //adding another char to the current string
        nameList[i][j] = getchar();
        size += sizeof(char);
    }
    j = 0;
    if (nameList[i][j] == 'Q')
    {
        if (nameList[i][j + 1] == 'U')
            if (nameList[i][j + 2] == 'I')
                if (nameList[i][j + 3] == 'T')
                    check++;
    }
    i++;
}
Evi
  • 1
  • 1
  • 1
    Please read through [ask] and [mcve]. You mention a runtime error, what is the code that causes that? We can help with specific problems that you've attempted to fix, but Stack Overflow is not a code-writing service. – user812786 Jan 06 '17 at 13:22
  • Ok, you should be able to do it with dynamic allocation of an array of pointers and strings. So you should show you current code and your error. – Serge Ballesta Jan 06 '17 at 13:23
  • I will down vote any answer that robs this student of his/her chance to learn. – Jonathon Reinhart Jan 06 '17 at 13:27
  • @Evi - you'll have more chance of getting help if you show what you have tried (i.e. code), what you expect that code to do, and any results you get (particularly if they differ from what you expect). – Peter Jan 06 '17 at 13:28
  • So you are allowed to use `string.h` library functions. In `string.h` the following two functions will be of great use to you: `strcmp()` and `strcpy()`. – Cherubim Jan 06 '17 at 14:41

1 Answers1

0

nameList = (char**)realloc(nameList, (size + sizeof(char)));

Uhm... Why are you using a '+' there? And you are looking for the size of the wrong sort of value as well.

This allocates an array of pointers. But what are the pointers pointing to here?

luckykaa
  • 166
  • 6
  • I am using a '+' because I want to add another memory at the size of char to the string each time I enter a letter as long as the string is not 'QUIT'. The main problem is that I don't know if that's the right way to do it, and if realloc should be used for both the list of arrays and for each string. Each pointer is supposed to point to each string that I've entered, and each of the strings is separated when the input is an enter. – Evi Jan 06 '17 at 16:03
  • The strings do need to be allocated separately. – luckykaa Jan 07 '17 at 17:50
  • Each item in the array is a pointer to a string. So nameList[0] is a pointer to a string and that string needs to be allocated. namelist[1] is a pointer to the next string and so on for nameList[2] to nameList[i]. For a beginner it can be quite hard to keep track of pointers to pointers, so I'm a bit worried this next bit of advice may confuse you. But you are still going to find a problem here. nameList is a list of "char*" (pointer to char) and not "char", you will need to use sizeof(char*) – luckykaa Jan 07 '17 at 17:56