Question

#include <stdio.h> #include <string.h> #define WORDLEN 20 char smallest_word[WORDLEN + 1], largest_word[WORDLEN + 1], word[WORDLEN +...

#include <stdio.h>

#include <string.h>

#define WORDLEN 20

char smallest_word[WORDLEN + 1],

largest_word[WORDLEN + 1],

word[WORDLEN + 1];

void get_first_word(void);

void get_another_word(void);

void get_word(void);

int main(void) {

get_first_word();

while (strlen(word) != 4)

get_another_word();

printf("\nSmallest word: %s\nLargest word: %s\n",

smallest_word, largest_word);

return 0;

}

void get_first_word(void) {

get_word();

strcpy(smallest_word, word);

strcpy(largest_word, word);

}

void get_word(void) {

printf("Enter word: ");

scanf("%20s", word);

}

void get_another_word(void) {

get_word();

if (strcmp(word, smallest_word) < 0)

strcpy(smallest_word, word);

else if (strcmp(word, largest_word) > 0)

strcpy(largest_word, word);

}

can you explain this code in c and the meaning

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

#define WORDLEN 20 //This is constant value through out the program.

char smallest_word[WORDLEN + 1],  

largest_word[WORDLEN + 1],

word[WORDLEN + 1];

Here we declare 3 variables, and these variables scope is global, So, they can be modified by any function.

----------------------------------------------

void get_first_word(void);

void get_another_word(void);

void get_word(void);

These are the function proto types, These functions are used in our program.

----------------------------------------------

int main(void) { // our program starts executing from here..

get_first_word(); // This is function call to get_first_word() ... so, this function will execute.

Function Definition of get_first_word():

void get_first_word(void) {

get_word(); // This is another function call..control goes to get_word()

strcpy(smallest_word, word); //copies string in word to smallest_word

strcpy(largest_word, word); //copies string present in word to largest_word.,

}

After Execution of this function, Control agian comes back to main().

---------------------------------

void get_word(void) {

printf("Enter word: ");

scanf("%20s", word);

}

Here we input the word from keyboard and since word is global variable, It can be accessed from any function.

------------------------------------------------

while (strlen(word) != 4)

get_another_word();

These lines indicate, it will call the function get_another_word() until the we read a string of length 4.

-----------------------

void get_another_word(void) {

get_word(); //This is nothing but reading string from keyboard and storing in word.

if (strcmp(word, smallest_word) < 0)

strcpy(smallest_word, word);

else if (strcmp(word, largest_word) > 0)

strcpy(largest_word, word);

}

This function compares string which we read recently and smallest_word string,

If strlen(word) < strlen(smallest_word), we replace the word with smallest_word.

else,

It compares string which we read recently and largest_word string,

If strlen(word) > strlen(largest_word), we replace the word with largest_word.

----------------------------

control comes back again to main().

This above function continues to call until w read a word of length of '4'.

------------------------

printf("\nSmallest word: %s\nLargest word: %s\n", smallest_word, largest_word);  

Here we are printing The smallest word and largest word.

--------------------

Screenshots of execution::

Enter word: Hello Enter word: Hellow Enter word: hell Smallest word: Hello Largest word: hell ... Program finished with exit

In the above example,

First we read the value Hello,

So, smallest_word contains "Hello" and largest_word also contains "Hello"

again we read the value , HelloW,

so, This time strlen(word) = 6 and strlen(largest_word) = 5.

so it will copy the value HelloW to largest_word.

Again we read the value beacuse while loop will execute until we read a word of length of 4.

Now we read word value as "hell"

Now , strlen(smallest_word) = strlen("Hello") = 5 and strlen(word) = strlen("hell") = 4,

So, 5<4 , so hell will be copied to smallest_word

As length of word is 4, The while loop breaks,

Printing smallest_word and largest_word which contains valuues "hell" and "HelloW" respectively.

-----------------

Summary:

The main aim is to find the smallest and largest length of the words that are read by user.

------------------------

Add a comment
Know the answer?
Add Answer to:
#include <stdio.h> #include <string.h> #define WORDLEN 20 char smallest_word[WORDLEN + 1], largest_word[WORDLEN + 1], word[WORDLEN +...
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
  • what is the output of the following program? #include<stdio.h> #include<string.h> int main(void){ char word[20]; int i...

    what is the output of the following program? #include<stdio.h> #include<string.h> int main(void){ char word[20]; int i =0    strcpy(word, "ORGANISE"); while(word[i] !='\0'){ if(i%2 ==1) word[i] = 'C'; i++; } printf("%s",word); return 0; }

  • #include <stdio.h> #include string.h     #define C17_PRICE 12.80 #define F25_PRICE 4.29 #define DN3_PRICE 10.00 #define GG7_PRICE...

    #include <stdio.h> #include string.h     #define C17_PRICE 12.80 #define F25_PRICE 4.29 #define DN3_PRICE 10.00 #define GG7_PRICE 35.00 #define MV4_PRICE 18.49 #define DISCOUNT        0.05 #define DISC_THRES    10 #define SAME_DAY_DELIVERY =   35.0 #define NEXT_DAY_DELIVERY =   20.0    typedef enum{ WRONG, C17, F25, DN3, GG7, MV4} part     /*--- Function Prototypes ------------*/ part getPartType ( void ); int getquantity ( void ); int getDeliveryOption ( void ); float calcPriceOfParts( part orderedPart, int quantity ); float calcTotalCarges ( float orderPrice, char...

  • 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);...

  • 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...

  • #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] !=...

  • #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)...

  • Question 6 Predict the output of the following question: 8 pts #include <stdio.h> #include<string.h> char al(5)={...

    Question 6 Predict the output of the following question: 8 pts #include <stdio.h> #include<string.h> char al(5)={ "gate", "kate","rate","date" ); void rec(char a[][5], int n){ if(n<= 1) return; char temp[5]; strcpy(temp'a);//copies the string stored in a into temp strcpy(*a,a[n-1]); strcpy(a[n-1),temp); rec(a+1,n-2); } void main() { rec(a 4); int i = 0; for(;i<4;i++) a[i][4]+= 10; printf("%s", a); }

  • C program help #include #include #include void PrintName(char firstname[16], char lastname[16]); int main () { char...

    C program help #include #include #include void PrintName(char firstname[16], char lastname[16]); int main () { char firstname[16]; char lastname[16]; printf("please enter your first name:"); scanf("%s",firstname); printf("please enter your last name:"); scanf("%s",lastname); PrintName(firstname,lastname); return 0; } void PrintName(char firstname[16], char lastname[16]){ char fullname[34]; *fullname=*firstname+*lastname; (char*) malloc (sizeof (*fullname)); if (strlen(firstname) > 16||strlen(lastname)>16||strlen(fullname)>16) fflush(stdin); else printf(" the full name is %s %s \n",firstname,lastname); printf(" the full name is-> %s",fullname);/*why is one not run*/ return 0; }

  • can u please solve it in c++ format,,,, general format which is #include<stdio.h> .......printf and scanf...

    can u please solve it in c++ format,,,, general format which is #include<stdio.h> .......printf and scanf format dont worry about the answers i have down Q3, What is the output of the program shown below and explain why. #include #include <stdio.h> <string.h> int main(void) ( char str[ 39764 int N strlen(str) int nunj for (int i-0; i t Nǐ.de+ printf(") num (str[] 'e); I/ digits range in the ASCII code is 48-57 if (nue > e) f 9 printf( Xdin,...

  • Explain the code and analyze the performance of algorithm #include<stdio.h> #include<string.h> #define NUM 1...

    Explain the code and analyze the performance of algorithm #include<stdio.h> #include<string.h> #define NUM 100 #define maxint 10000 void dijkstra(int n,int v,int dist[],int prev[],int c[][NUM]) {    int i,j;    bool s[NUM];    for(i=1; i<=n; i++)    {        dist[i] = c[v][i];        s[i] = false;        if (dist[i]>maxint) prev[i] = 0;        else prev[i] = v;    }    dist[v] = 0;    s[v] = true;    for(i=1; i<n; i++)    {        int tmp = maxint;        int u = v;        for(j=1; j<=n; j++)            if(!(s[j]) && (dist[j]<tmp))            {                u = j;                tmp = dist[j];           ...

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