Question

In: Computer Science

How do I sort something alphanumerically in C?   consider the example 10abc, 2abc, 2abbbb,b500, a500cd the...

How do I sort something alphanumerically in C?  

consider the example 10abc, 2abc, 2abbbb,b500, a500cd

the output should be a500cd < b500 < 10abc < 2abbbb < 2abc.

another example would be  A < AB < A3D < 3DA < 5.  

elements are in a linked list

Solutions

Expert Solution

Hi

I have a simple solution or you, see below alogorithm to sort strings, similiarly you can do in it for link list

str[0] will represent one node of list and str[1] is another node of the link list and so on....., please let me know for any clarifications i can help you

step 1:

char str[] ={"b500","a500cd"}

int i = 0, j =0;

for(i=0;i<=2;i++)
      for(j=i+1;j<=2;j++){
         if(strcmp(str[i],str[j])>0){
            strcpy(temp,str[i]);
            strcpy(str[i],str[j]);
            strcpy(str[j],temp);
         }
      }

//Revised Answer ,

Now the issue here is actual value of character 0 -9 is less than a - z in he ascii chart so you will face this problem ,

so what we can do is when we get a character from 0 - 9 we will add 123 which is value after 'z' , so the value becomes greater than z and then we can compare the characters ,so now i have revised the alogorithm as below.

for(int i =0 i < count ; i ++){

for( int j =0 ;j < strlen(str[i]); j ++){

char temp1 = str[i][j];

char temp2 = str[i+1][j];

if(temp1 > ='0' && temp1 < ='9'){

temp1 = temp 1 + 123;

}

if(temp2 > ='0' && temp2 < ='9'){

temp2 = temp2 + 123;

}

if (temp1> temp2){

strcpy(temp,str[i]);

strcpy(str[i],str[i+1]);

strcpy(str[i+1],temp);

}else if( temp1 < temp2){

continue;

}else if (temp1 == temp2){

break;

}

}

}   

  


Related Solutions

How do I check something is analytic. C-R is necessary but not sufficient. i was told...
How do I check something is analytic. C-R is necessary but not sufficient. i was told that I must also check if the partial derivatives are continuous. How would I do that?
i have an array of strings, how do i sort them into a binary tree? C...
i have an array of strings, how do i sort them into a binary tree? C Program
c++ How do I get numbers and words before I press the enter? For example, if...
c++ How do I get numbers and words before I press the enter? For example, if i type cat 12 Rabbit 27 and then click enter, i want to stop receiving input.
How can i bubble sort a sentence in a char array in c++ This is the...
How can i bubble sort a sentence in a char array in c++ This is the prototype of the function: char* sort(char string[], int numOfWords, int lengthOfWord); This is the testing code in the main file: char words[] = "CAT FAT BAT HAT RAT"; printf("Before sort: t%s\n";words); char result = sort(words; 5; 3); printf("After sort : t%s\n"; result); Expected output: Before sort: CAT FAT BAT HAT RAT After sort: BAT CAT FAT HAT RAT
c++ How do I get it before a particular string when I use <ifstream>? For example,...
c++ How do I get it before a particular string when I use <ifstream>? For example, when it's 'Apple: fruit', I only want to get the previous ':' text (Apple). And then I want to move on to the next row. How can i do?
C++ --------------------------------------------- Do a comparison of a slow sort with Big O(n2) (selection sort, insertion sort,...
C++ --------------------------------------------- Do a comparison of a slow sort with Big O(n2) (selection sort, insertion sort, or bubble sort) and one faster sort of Big O(n * log n) (mergesort or quicksort). Count the number of moves (a swap counts as one move). With mergesort, you can count the range of the part of the array you are sorting (i.e. last-first+1). Use the code from the textbook (copy from the lecture notes) and put in an extra reference parameter for...
C++ How would I sort my output to evaluate it by sorting the column by it's...
C++ How would I sort my output to evaluate it by sorting the column by it's size? I didn't include my full program but here is the main.cpp where i'm supposed to sort the output by column size. //User Libraries #include <cstdlib> #include <ctime> #include <iostream> using namespace std; //User Libraries #include "Table.h" #include "Triangle.h" //Global Constants //Function Prototype void prntRow(RowAray *,int); void prntTab(Table *); void prntTri(Triangle *); //Execution Begins Here! int main(int argc, char** argv) { //Initialize the random...
How would I add a quickSort function to the below C++ code to sort the randomly...
How would I add a quickSort function to the below C++ code to sort the randomly generated numbers? #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int i; int array[10]; int odd; int Max; int counter = 0; int main() { cout << "The 10 random elements are: "; cout << endl; srand ( time(0) ); for (int j = 0; j < 99; j++) { i = rand() % 100; if (i != i - 1) array[j] =...
Give an example of how Radix sort works
Give an example of how Radix sort works
For this assignment, find out how to do a bubble sort, selection sort, or insertion sort...
For this assignment, find out how to do a bubble sort, selection sort, or insertion sort in Java. You have the option to choose but you must label (with comments) the algorithm you choose to implement. Convert that algorithm to a generic algorithm and constraint it to only using numerics. Your method should accept an array as a parameter and sort the content of the array. If you wish, you can throw an exception if the contents of the array...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT