0

I have a character array

char word[30]; 

that keeps a word that the user will input and I want to sort the letters for example if the word is "cat" I want it to make it a "act" I suppose is rather easy task but as a beginner in C programming i find the examples on the internet rather confusing.

This is my code trying to do the bubble sort...

Still doesn't work

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

#define MAX_STRING_LEN 30

main()
{
char w1[30], w2[30];
char tempw1[30], tempw2[30];
int n,i,k;
char temp;  
    printf("Give the first word: ");
    scanf("%s",&w1);
    printf("Give the second word: ");
    scanf("%s",&w2);
        if(strlen(w1)==strlen(w2)) /* checks if words has the same length */
            {
            strcpy(tempw1,w1); /*antigrafei to wi string sto tempw1 */ 
            strcpy(tempw2,w2); /*antigrafei to w2 string sto tempw2 */ 
            n=strlen(w1);


             for (i=1; i<n-1; i++)
               {
                                for (k=n;k>i+1;k--)
                    {
                        if (w1[k] < w1[k-1])
                            {
                                temp=w1[k-1];
                                w1[k-1]=w1[k];
                                w1[k]=temp;
                            }
                     }
            }
             for (i=1; i<n-1; i++)
               {
                                for (k=n;k>i+1;k--)
                    {
                        if (w2[k] < w2[k-1])
                            {
                                temp=w2[k-1];
                                w2[k-1]=w2[k];
                                w2[k]=temp;
                            }
                     }
                } 
            printf("%s \n",tempw1);
            printf("%s \n",w1);
            printf("%s \n",tempw2);
            printf("%s \n",w2);
            /* call qsort */
            /* call compare */
            }
        else printf(" \n H lexh %s den einai anagrammatismos tis lexhs %s",w1,w2);
    return 0;St
3
  • one of the easier sorting algorithms is bubble sort. Commented Feb 16, 2013 at 14:11
  • ok thank you for the bubble sort suggestion i thought that this will only work in integers! Ill try it right away , the programm i am trying to work is an anagram game as an excersise i have solved all the other parts that i compare the two arrays but needed to sort them first to compare. Commented Feb 16, 2013 at 14:19
  • @poseidon11 1. characters are integers. 2. You can come up with some sort of algorithm that compares objects based on their properties, else there wouldn't be generic sorting algorithms. Commented Feb 16, 2013 at 14:22

2 Answers 2

10

Use qsort() from the C standard library:

int compare(const void *a, const void *b)
{
    return *(const char *)a - *(const char *)b;
}

char arr[] = "dbaurjvgeofx";

printf("Unsorted: %s\n", arr);
qsort(arr, strlen(arr), 1, compare);
printf("Sorted: %s\n", arr);
Sign up to request clarification or add additional context in comments.

Comments

1

This is fundamental programming knowledge and you'd be doing yourself a favor by solving it yourself..

That being said, here's a quick pseudo

for i is equal to 1 to length of array
  for k is equal to i to length of array
   if i > k
    temp = i
    i = k
    k = temp
   endif
  endfor
endfor

2 Comments

This isn't quick sort. Oh--after rereading I guess maybe he wasn't looking for the quick sort algorithm... just a "quick" sort. Lol.
thanks alot for the help i tryed bubble sort all day but i couldnt do it for some reason , finaly got it working with qsort :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.