Search code examples
arrayscsyntax

How can I get rid of the brackets used to initalize my array?


I have to create a program in C that asks for the user to input 80 or fewer characters and then alphabetize them using two functions outside of the main(). (for example "d c b a" would turn into "a b c d"). I also can't use any square brackets with my arrays or anywhere in my code. I managed to get rid of all brackets except initializing the array and the sorting function. I can't figure out how to do that.

This is some of the code from the main:

char *str[size];
for (int i = 0; i < size; i++) {
    printf("Enter single character %d: ", i+1);
    str[i] = (char*)malloc(size);
    scanf(" %c", *(str+i));
}
sort(str, size);

And this is my function for sorting:

void sort(char *str[], int size) {
   for (int i = 0; i < size; i++) {
      for (int j = i+1; j < size; j++)
         if (strcmp(str[i], str[j]) > 0) {
            swap(str[i],str[j]);
         }
   }
}

I tried:

char *str=(char*)malloc(80*sizeof(char));

But I got errors with the various *(str+i) throughout my code and sort(str, size);.


Solution

  • I get the impression that the purpose of this assignment is to get very familiar with pointers and the incrementing and decrementing of pointers in lieu of utilizing fairly conventional coding with array indices.

    With that in mind, and reviewing your initial coding here is some refactored code to use as food for thought.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void sortStrg(char *text)
    {
        int swap = 0;
        char temp1, temp2, temp3, *work;
    
        while(1)
        {
            work = text;
            for (int i = 0; i < (strlen(text) - 1); i++)    /* Very crude and repetitive bubble sort - checking two adjancent characters at a time */
            {
                temp1 = *work;                              /* Increment and decrement the character pointer to work through the memory locations  */
                work++;
                temp2 = *work;
                if (temp1 > temp2)
                {
                    temp3 = temp1;
                    temp1 = temp2;
                    temp2 = temp3;
                    swap  = 1;
                    //printf("Swapping occurs\n");
                }
                work--;
                *work = temp1;
                work++;
                *work = temp2;
            }
    
            if (swap == 0)
            {
                break;
            }
            else
            {
                swap = 0;
            }
        }
    
        return;
    }
    
    int main()
    {
        char *text,  *work;
    
        text = malloc(sizeof(char) * 81);
    
        printf("Enter some text: ");
    
        if(fgets(text, 80, stdin) == 0)
            return 1;
    
        work = text;
    
        for (int i = 0; i < 80; i++)   /* Since fgets is being used, replace the possible newline character with a "0" terminator */
        {
            if (*work == '\n')
            {
                *work = '\0';
                break;
            }
            work++;
        }
    
        printf("This is what was entered: %s\n", text);
    
        sortStrg(text);
    
        printf("Sorted string: %s\n", text);
    
        free(text);
        return 0;
    }
    

    Following are some key points:

    • Per the requirements, the character array (string) is defined via the definition of a character pointer and utilizing of the "malloc" function.
    • Multiple character pointers are utilized that effectively reference the same block of memory for the character array.
    • In lieu of referencing an array element (e.g. text[0]) navigation of the array is performed with incrementing and decrementing the pointer memory location.
    • Sorting is performed utilizing a simple brute force bubble sort method.

    Following is some test input and output at the terminal demonstrating the string sorting function.

    craig@Vera:~/C_Programs/Console/SortString/bin/Release$ ./SortString 
    Enter some text: euytopdghiiwpnnbs
    This is what was entered: euytopdghiiwpnnbs
    Sorted string: bdeghiinnoppstuwy
    craig@Vera:~/C_Programs/Console/SortString/bin/Release$ ./SortString 
    Enter some text: dbca
    This is what was entered: dbca
    Sorted string: abcd
    craig@Vera:~/C_Programs/Console/SortString/bin/Release$ ./SortString 
    Enter some text: d c b a
    This is what was entered: d c b a
    Sorted string:    abcd
    

    It can be seen that characters are sorted in ascending order. One thing to note is that in your narrative you gave an example of 'd c b a' would sort to be 'a b c d'. This refactored iteration just considers a space as another character to be sorted. If "space integrity" has to be considered, then there would need to be additional logic added for that. However, this refactored code is just to provide a "proof of principle".

    Along with your review of this code, it definitely would be advisable to review some tutorial literature that covers the usage and modification of pointers.