Question

In: Computer Science

IN PROGRAMMING LANGUAGE C -I am trying to alphbetize a string in descending or to EX...

IN PROGRAMMING LANGUAGE C

-I am trying to alphbetize a string in descending or to

EX

INPUT: B C D A

OUTPUT:

D

C

B

A

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

int main(int argc, char*argv[])
{


        int MAX = 100000;
        int i =0;
        int k =0;
        int j =0;
        char array[MAX];
        char split[] = " ,.-!?()0123456789";
        int n = 0;
        char second[MAX];
        printf("Please enter in a String: ");
        //String input
        fgets(array,MAX,stdin);

        char **first =(char**)malloc(sizeof(char*));
        char *token = strtok(array,split);

        while(token!= NULL)
        {

                k = strlen(token);
                first=(char **)realloc(first,(n+1)*sizeof(char));
                first[n] = (char *)malloc((k+1)*sizeof(char));
                memcpy(first[n],token,k*sizeof(char));
                token = strtok(NULL,split);
                n++;

        }

        second = sortedArray(first,n);
        for(i =0; i<n;i++)
        {
         printf("%s",second[i]);
        }


        return 0;

}

char *sortedArray(char *sortRay[], int size)
{
        int i =0;
        int j =0;
        char temp[300];
        for(i =0; i<size; i++)
        {
          for(j =i +1; j<size;j++)
           {
             if(strcmp(sortRay[i],sortRay[j])>0)
            {
             strcpy(temp,sortRay[i]);
             strcpy(sortRay[i],sortRay[j]);
             strcpy(sortRay[j],temp);
            }
           }
        }
        return sortRay;
}

Solutions

Expert Solution

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

int main (void) {
int MAX = 100000;
char string[MAX];
char temp;
int i, j;
printf("Please enter in a String: ");
fgets(string,MAX,stdin);
printf("String before sorting - %s ", string);
int n = strlen(string);
for (i = 0; i < n-1; i++) {
for (j = i+1; j < n; j++) {
if (string[i] < string[j]) {
temp = string[i];
string[i] = string[j];
string[j] = temp;
}
}
}

printf("String after sorting - %s ", string);
return 0;
}


Related Solutions

In C programming, I am trying to search for the names of people that in this...
In C programming, I am trying to search for the names of people that in this DOISigned.txt file, however I am having trouble getting the first and last names of the multiple people named john, my current code only searches for John once and then it terminates,here is my current code #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> #include <stdlib.h> #define BUF_SIZE 0x3000 char buf[BUF_SIZE]; int main() {    char* inputFile = "DOISigners.txt";    FILE* fp;    fp = fopen(inputFile, "r");...
C programming language. **I am aware that I am only supposed to ask one question so...
C programming language. **I am aware that I am only supposed to ask one question so if you cant do all of this could you please do part 2? thank you! This lab, along with your TA, will help you navigate through applying iterative statements in C. Once again we will take a modular approach to designing solutions to the problem below. As part of the lab you will need to decide which C selection structure and iterative structure is...
C programming language. **I am aware that I am only supposed to ask one question so...
C programming language. **I am aware that I am only supposed to ask one question so if you cant do all of this could you please do part 3? thank you! This lab, along with your TA, will help you navigate through applying iterative statements in C. Once again we will take a modular approach to designing solutions to the problem below. As part of the lab you will need to decide which C selection structure and iterative structure is...
I am building a game in C programming language where I need to add objects of...
I am building a game in C programming language where I need to add objects of various length into a game board. The game board is 8X8 and we must account for the boundaries for the board and not go over them with our objects. The boards upper left corner is at 0x0 and we must return 1 if it fits and -1 if it does not fit. I have the following 2 functions to start with: ```int add_object_vert(int r,...
I have a question about C++ programming Language class. I am confused with these terms and...
I have a question about C++ programming Language class. I am confused with these terms and what they are for. 1. Unix 2. Terminal 3. Git 4. CLOC 5. Linux Please explain each words and what's their jobs for C++. Also, if you know some good sources/websites, could you please provide me a link where I can learn how to use Unix, Terminal, Git, etc.
Hi, I am working on an assignment in C-Programming language dealing with LInked lists, in the...
Hi, I am working on an assignment in C-Programming language dealing with LInked lists, in the code there is instructions on where to write the code. I do not know how to write Linked Lists. Has to be in the C-language, Any help is greatly appreciated   //agelink.c //maintains list of agents //uses linked list #include <stdio.h> #include <stdlib.h> #define TRUE 1 void listall(void); void newname(void); void delink(void); void memexit(void); void wfile(void); /********************************************************************* this is the structure to hold a agent...
I am trying to return a string from certain position of the sentence on C++, like...
I am trying to return a string from certain position of the sentence on C++, like a function/ statement that is equivalent to excel mid function.
I am trying to tokenize a string using a function by passing the char string[] and...
I am trying to tokenize a string using a function by passing the char string[] and char *pointer[100]. While I have working code inside the int main(), I am having trouble actually declaring the parameters for the function. I know how to pass the char array (char string[]), but not how to pass the char pointer array (char *pointer[100]). This is my code below: int main() {    // Declare variables    char str[] = "this is a test only...
This is for Python programming, and I am trying to use 2 for loops to ask...
This is for Python programming, and I am trying to use 2 for loops to ask a slaesperson how many of 5 different items they sold, then the program is to calculate the total dollar amount sold. Formatting and all that aside, I am having an issue with the loops not stepping through the 2 lists at the same time. The first loop is taking all 5 entered quantities times the price of the first item, and then all 5...
Left shift. I am trying to left shift a string located in a text file. I...
Left shift. I am trying to left shift a string located in a text file. I am using c++ and the goal is to left shift by 1 character a string of length N, and output on stdout all of the performed shifts. Can someone tell me what I am doing wrong, and tell me what I can fix? i.e: text file contains: Hi there!. Output on stdout: Hi there!, i there!H, _there!Hi, there!Hi_,...., !Hi_there. #here "_" represent space. -------------------------------------------------------------------------------------------------------------------------...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT