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

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
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?
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...
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...
How do I sort a sequence A = 7,2,3,14,2,8,8,3,7,10 in O(nlogm) time where n is size...
How do I sort a sequence A = 7,2,3,14,2,8,8,3,7,10 in O(nlogm) time where n is size of A and m is the number of distinct elements? No code, just a good explanation will do
How would I make a bubble sort and an optimized bubble sort with the code given?...
How would I make a bubble sort and an optimized bubble sort with the code given? I also need to implement a timer into each sort and display runtime with the sorts. NODE.H _______________________________________________________________________________________________________ /* node.h */ /* two classes 1: node.h 2. singlylinkedlist.h nod1 (value + pointer) ---> node2 ---> node3 ---> |||| <--- node.h ^ | singlylinkedlist ----------------*node head; */ #ifndef NODE_H #define NODE_H #include <iostream> using namespace std; class Node {    friend class singlyLinkedList; public:   ...
how do i run 3 files on c++?
how do i run 3 files on c++?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT