user15709472
user15709472

Reputation:

How to sort the letters of a string alphabetically?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void function(char *q) {
    char temp, *p;
    int i;

    while(*q) {
        p=q;
        i=1;

        while(*p) {
            if(*p > *(p+i)) {
                temp=*(p+i);
                *(p+i)=*p;
                *p=temp;
            }
            i++;
            p++;
        }
        q++;
    }
}

int main() {
    char str[100]="bonjour";
    function(str);
    printf("%s", str);

    return 0;
}

I've been struggling with this code for a while, and I don't know where I am mistaken. I wanted to print a given string in alphabetic order. My program is supposed to swap the previous character with the next one if the the previous character is greater than the next one, but it didn't work at all.

Would you please tell me where did I make a mistake?

Upvotes: 2

Views: 154

Answers (3)

fares
fares

Reputation: 93

more beautiful way

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define size 100 
void function(char *q) {
    
    int len = strlen(q);
     //finds the length of the string(char array);
    
    for (int i = 0; i < len; i++)
    {
        for (int j = i+1; j < len; j++)
        {
            if (q[j] < q[i])    // performs a simple swap
            {
                 q[i]=q[i]^q[j]; //use xor
                  q[j] = q[i]^q[j];
                q[i] = q[i]^q[j];
               
            }
        }
    }
}

int main() 
{
 char str[size]="bonjour";
  
    function(str);
    printf("%s", str);

return 0;
}

Upvotes: 0

Siddhant
Siddhant

Reputation: 646

A simple bubble sort will do, but its the least efficient sorting algorithm. You can use other sorting algorithms like merge sort or quick sort for much better efficiency, but that's on you.

below is the code using bubble sort :-

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void function(char *q) {
    
    int len = 0;
    while (q[len] != '\0')  //finds the length of the string(char array);
    {
       len++;
    }
    
    for (int i = 0; i < len; i++)
    {
        for (int j = i+1; j < len; j++)
        {
            if (q[j] < q[i])    // performs a simple swap
            {
                char temp = q[i];
                q[i] = q[j];
                q[j] = temp;
            }
        }
    }
}

int main() 
{
    char str[100]="bonjour";
    function(str);
    printf("%s", str);//bjnooru

return 0;
}

Upvotes: 0

Amal K
Amal K

Reputation: 4929

You don't need i. You always have to compare to p+1 since you have to check the values of 2 adjacent elements. Also, inside the second while loop, use while(*(p + 1)), because you only need to compare n-1 times as a single comparison includes two elements.

void function(char *q)
{
    char temp, *p;
    while (*q)
    {
        p = q;
        while (*(p + 1))
        {
            if (*p > *(p + 1))
            {
                temp = *(p + 1);
                *(p + 1) = *p;
                *p = temp;
            }
            p++;
        }
        q++;
    }
}
OUTPUT:
bjnooru

Upvotes: 0

Related Questions