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.
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. -------------------------------------------------------------------------------------------------------------------------...
I am trying to figure out the best way to solving a problem in the language...
I am trying to figure out the best way to solving a problem in the language python. I have some but have no clue if I am even going in the right direction. Here are the instructions: Write a program that calculates the shopping list for a birthday party with the minimum amount of leftovers. The program should ask the user for the number of kids attending the party. Assume each kid will cook (but not necessarily eat) 2 hot...
in C programming language char character [100] = "hello"; a string array variable It is given....
in C programming language char character [100] = "hello"; a string array variable It is given. By writing a function called TranslateString, By accessing the pointer address of this given string, returning the string's address (pointer address) by reversing the string Write the function and use it on the main function. Function void will not be written as. Return value pointer address it will be. Sweat operation on the same variable (character) It will be made. Declaration of the function...
Programming Language: C# Person Class Fields - password : string Properties + «C# property, setter private»...
Programming Language: C# Person Class Fields - password : string Properties + «C# property, setter private» IsAuthenticated : bool + «C# property, setter absent» SIN : string + «C# property, setter absent» Name : string Methods + «Constructor» Person(name : string, sin : string) + Login(password : string) : void + Logout() : void + ToString() : string Transaction Class Properties + «C# property, setter absent » AccountNumber : string + «C# property, setter absent» Amount : double + «C#...
The following is for C programming language: I want to scan for initials in a line...
The following is for C programming language: I want to scan for initials in a line of text. my line of text is as follows: 12345 3.5000 a j 12346 4.1000 s p The first number represents the student ID, the second number represents the gpa, the third character represents the first initial and the fourth character represents the last initial of the student. My text file contains these values. The following is my code: fscanf(fp, "%d %c %c", &studentID,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT