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