Question

Please explain how these code run for each line #include<stdio.h> #include<string.h> /** * Part A */...

Please explain how these code run for each line

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

/**
* Part A
*/
struct myWord{
   char Word[21];
   int Length;
};
int tokenizeLine(char line[], struct myWord wordList[]);
void printList(struct myWord wordList[], int size);
void sortList(struct myWord wordList[], int size);

/**
* main function
*/
int main()
{
   struct myWord wordList[20];
   char line[100];
   printf("Enter an English Sentence:\n");
   gets(line);
   int size = tokenizeLine(line, wordList);
   printf("\n");
   printf("Unsorted word list.\n");
   printList(wordList, size);
   sortList(wordList, size);
   printf("\n");
   printf("Sorted word list\n");
   printList(wordList, size);
}

/**
* Part B
*
* Function to tokenize the entered line and store each word and its size
* in struct object variables.
*/
int tokenizeLine(char line[], struct myWord wordList[])
{
   char *token = strtok(line, " .?!");
   int indx = 0;
  
   while(token != NULL)
   {
       strcpy(wordList[indx].Word, token);
       wordList[indx].Length = strlen(token);
       token = strtok(NULL, " .?!");
       indx++;
   }
   return indx;
}

/**
* Part B
*/
void printList(struct myWord wordList[], int size)
{
   int i;

   for(i = 0; i < size; i++)
   {
       printf("%s: %d\n", wordList[i].Word, wordList[i].Length);
   }
}


/**
* Part C
*
* Function to sort the list based on the length of the words
* using bubble sort
*/
void sortList(struct myWord wordList[], int size)
{
   int i, j;
   for(i = 0; i < size-1; i++)
   {
       for(j = 0; j < (size - i - 1); j++)
       {
           if(wordList[j].Length > wordList[j+1].Length)
           {
               struct myWord temp = wordList[j];
               wordList[j] = wordList[j+1];
               wordList[j+1] = temp;
           }
       }
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1


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

/**
* Part A
*/
/* Creating a Structure named myWord
* which holds word and the corresponding word length
*/
struct myWord{
   //This will hold the word
char Word[21];
//This will hold the word length
int Length;
};

//Function declarations
int tokenizeLine(char line[], struct myWord wordList[]);
void printList(struct myWord wordList[], int size);
void sortList(struct myWord wordList[], int size);

/**
* main function
*/
int main()
{
   //Creating a Struct Array of size 20
struct myWord wordList[20];

//This will hold the user entered input sentence
char line[100];

//Getting the input sentence entered by the user
printf("Enter an English Sentence:\n");
gets(line);

//calling the function by passing the user entered input sentence and struct array
int size = tokenizeLine(line, wordList);
printf("\n");
//Displaying the output
printf("Unsorted word list.\n");
//calling the function (so this will display contents of struct array before sort)
printList(wordList, size);

//Calling the function to sort the struct array instances based on the contents of word length
sortList(wordList, size);
printf("\n");
printf("Sorted word list\n");
//calling the function (so this will display contents of struct array after sort)
printList(wordList, size);
}

/**
* Part B
*
* Function to tokenize the entered line and store each word and its size
* in struct object variables.
*/
int tokenizeLine(char line[], struct myWord wordList[])
{
// Parsing the sentence when ever space , full stop    , question mark or Esclamation character occurs
char *token = strtok(line, " .?!");
int indx = 0;
  
//This while loop will continue to execute until the end of the line
while(token != NULL)
{
    //We called the each parsed word from sentence as Token , and store that token in struct word
strcpy(wordList[indx].Word, token);
//We store the length of the corresponding parsed word in struct length
wordList[indx].Length = strlen(token);

//Again getting the next word from the sentence
token = strtok(NULL, " .?!");

indx++;
}
return indx;
}

/**
* Part B
* This function will print the contents of the struct array
*/
void printList(struct myWord wordList[], int size)
{
int i;

//This for loop will display all the contents of the struct array (words and its length)
for(i = 0; i < size; i++)
{
printf("%s: %d\n", wordList[i].Word, wordList[i].Length);
}
}


/**
* Part C
*
* Function to sort the list based on the length of the words
* using bubble sort
*/
void sortList(struct myWord wordList[], int size)
{
int i, j;
for(i = 0; i < size-1; i++)
{
for(j = 0; j < (size - i - 1); j++)
{
if(wordList[j].Length > wordList[j+1].Length)
{
struct myWord temp = wordList[j];
wordList[j] = wordList[j+1];
wordList[j+1] = temp;
}
}
}
}

______________________________

Output:

________________________Thank You

Add a comment
Know the answer?
Add Answer to:
Please explain how these code run for each line #include<stdio.h> #include<string.h> /** * Part A */...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here....

    #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here. */ int GetNumOfNonWSCharacters(const char usrStr[]) { int length; int i; int count = 0; char c; length=strlen(usrStr); for (i = 0; i < length; i++) { c=usrStr[i]; if ( c!=' ' ) { count++; } }    return count; } int GetNumOfWords(const char usrStr[]) { int counted = 0; // result // state: const char* it = usrStr; int inword = 0; do switch(*it)...

  • Need help for C program. Thx #include <stdio.h> #include <string.h> #include <ctype.h> // READ BEFORE YOU...

    Need help for C program. Thx #include <stdio.h> #include <string.h> #include <ctype.h> // READ BEFORE YOU START: // This homework is built on homework 06. The given program is an updated version of hw06 solution. It begins by displaying a menu to the user // with the add() function from the last homework, as well as some new options: add an actor/actress to a movie, display a list of movies for // an actor/actress, delete all movies, display all movies,...

  • ​what's wrong with my code?????? #include <stdio.h> #include <stdlib.h> #include <string.h>                         &

    ​what's wrong with my code?????? #include <stdio.h> #include <stdlib.h> #include <string.h>                            #define MAXBINS 99 #define MAXCORS 26 #define MAXCUS 100 #define MAXPUR 10 /* datatype for a list of orders ----------------- */ typedef struct { int bin_order_number; char bin_order_char; }bin_order; typedef struct { int orderNo; bin_order orderbin; }order; typedef struct { int cusnumber; int n;   /* number of the orders what the txt include */ order oo[MAXPUR+1]; }customer; typedef struct{ int n; /*number of the customers */ customer cc[MAXCUS+1];...

  • #include<stdio.h> #include<stdio.h> int main(){ int i; //initialize array char array[10] = {“Smith”, “Owen”, “Kowalczyk”, “Glass”, “Bierling”,...

    #include<stdio.h> #include<stdio.h> int main(){ int i; //initialize array char array[10] = {“Smith”, “Owen”, “Kowalczyk”, “Glass”, “Bierling”, “Hanenburg”, “Rhoderick”, “Pearce”, “Raymond”, “Kamphuis”}; for(int i=0; i<8;i++){ for(int j=0; j<9; j++){ if(strcmp(array[j],array[j+1])>0){ char temp[20]; strcpy(temp,array[j]); strcpy(array[j],array[j+1]); strcpy(array[j+1],temp); } } } printf(“---------File Names---------\n”); for(inti=0; i<9; i++){ printf(“\t%s\n”,array[i]); } printf(-------5 Largest Files according to sorting----\n”); for(int i=0;i>=5;i--) { printf(“\t%s\n”,array[i]); } return0; } Consider the "sort" program (using with void* parameters in the bubblesort function) from the week 10 "sort void" lecture. Modify it as follows...

  • #include <stdio.h> #include<string.h> int main() { char strText[100] ="Start"; char i; int nTextASCIISum = strText[0] +...

    #include <stdio.h> #include<string.h> int main() { char strText[100] ="Start"; char i; int nTextASCIISum = strText[0] + strText[1] + strText[2]; int nTextLen = strlen(strText); int count = 0; printf("Welcome to token generator!\n"); printf("Enter a word to use in the token generator.\n You may enter as many words as you like. \n Press q and key when finished.\n"); scanf("%c", &strText[i]); //compute when to stop loop //check nTextLen == 1 and strText[0] == 'q' for(i=0;i< nTextLen;i++) if ((strlen(strText) != 1) || (strText[0] !=...

  • Please fill in this code to reverse a linked list: (written in C/C++) #include #include #include...

    Please fill in this code to reverse a linked list: (written in C/C++) #include #include #include using namespace std; /* Link list node */ struct Node { int data; // your code here }; /* Function to reverse the linked list */ static void reverse(struct Node** head_ref) { // your code here } /* Function to push a node */ void push(struct Node** head_ref, int new_data) { // your code here } /* Function to print linked list */ void...

  • Finish function to complete code. #include <stdio.h> #include <stdlib.h> #include<string.h> #define Max_Size 20 void push(char S[],...

    Finish function to complete code. #include <stdio.h> #include <stdlib.h> #include<string.h> #define Max_Size 20 void push(char S[], int *p_top, char value); char pop(char S[], int *p_top); void printCurrentStack(char S[], int *p_top); int validation(char infix[], char S[], int *p_top); char *infix2postfix(char infix[], char postfix[], char S[], int *p_top); int precedence(char symbol); int main() { // int choice; int top1=0; //top for S1 stack int top2=0; //top for S2 stack int *p_top1=&top1; int *p_top2=&top2; char infix[]="(2+3)*(4-3)"; //Stores infix string int n=strlen(infix); //length of...

  • Rewrite this code in Java please #include <iostream> #include <fstream> #include <cstdlib> #include <ctime> using namespace...

    Rewrite this code in Java please #include <iostream> #include <fstream> #include <cstdlib> #include <ctime> using namespace std; long length = 1000; const long max_length = 100000; int list[max_length]; void read() {     ifstream fin("random.dat", ios::binary);     for (long i = 0; i < length; i++)     {         fin.read((char*)&list[i], sizeof(int));     }     fin.close(); } void bubbleSort() {     int temp;     for(long i = 0; i < length; i++)     {         for(long j = 0; j< length-i-1; j++)...

  • IN C ONLY As mentioned earlier there are two changes we are going to make from...

    IN C ONLY As mentioned earlier there are two changes we are going to make from lab 5, The file you read into data structures can be any length. studentInfo array will be stored in another struct called studentList that will contain the Student pointer and current length of the list. Sometimes data can be used in structs that correlate between variables so it's convenient to store the data in the same struct. Instead of tracking a length variable all...

  • #include <stdio.h> #include <stdlib.h> #include <strings.h> #include <unistd.h> #include <pthread.h> pthread_mutex_t mtx; // used by each...

    #include <stdio.h> #include <stdlib.h> #include <strings.h> #include <unistd.h> #include <pthread.h> pthread_mutex_t mtx; // used by each of the three threads to prevent other threads from accessing global_sum during their additions int global_sum = 0; typedef struct{ char* word; char* filename; }MyStruct; void *count(void*str) { MyStruct *struc; struc = (MyStruct*)str; const char *myfile = struc->filename; FILE *f; int count=0, j; char buf[50], read[100]; // myfile[strlen(myfile)-1]='\0'; if(!(f=fopen(myfile,"rt"))){ printf("Wrong file name"); } else printf("File opened successfully\n"); for(j=0; fgets(read, 10, f)!=NULL; j++){ if (strcmp(read[j],struc->word)==0)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT