Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C Exercises: Read a list of pairs of a word and a page number, and prints the word and a list of the corresponding page numbers

C Basic Declarations and Expressions: Exercise-149 with Solution

Write a C program, which reads a list of pairs of a word and a page number, and prints the word and a list of the corresponding page numbers.

The number of pairs of a word and a page number is less than or equal to 1000. A word never appears in a page more than once. The words should be printed in alphabetical order and the page numbers should be printed in ascending order.

Input:
word page_number
Output:
word
a_list_of_the_page_number
word
a_list_of_the_Page_number

Sample Solution:

C Code:

#include <stdio.h>
#include<string.h>
typedef struct{
  int page_no;
  char word[50];
}STR;
main(){
  STR temp,str[10000];
  int i=0,j,k;
  int count=0;
  printf("Input pairs of a word and a page_no number:\n"); 
  while(scanf("%s %d",str[i].word,&str[i].page_no)!=EOF){
    i++;
  }
  for(j=0;j<i;j++){
    for(k=i-1;0<k;k--){
      if(strcmp(str[k].word,str[k-1].word)<0){
	temp=str[k];
	str[k]=str[k-1];
	str[k-1]=temp;
      }
      else if(strcmp(str[k].word,str[k-1].word)==0){
	if(str[k].page_no<str[k-1].page_no){
	  temp=str[k];
	  str[k]=str[k-1];
	  str[k-1]=temp;
	}
      }
    }
  }
  printf("\nWord and page_no number in alphabetical order:\n"); 
  for(j=0;j<i;j++){
    if(j!=0){
      if(strcmp(str[j].word,str[j-1].word)==0){
	printf(" %d",str[j].page_no);
      }
      else{
	printf("\n%s\n%d",str[j].word,str[j].page_no);
      }
    }
    else{
      printf("%s\n%d",str[j].word,str[j].page_no);
    }
  }
  printf("\n");
  return 0;
}

Sample Output:

Input pairs of a word and a page_no number:
Twinkle
65
Twinkle
55
Little
25
Star
35
^Z

Word and page_no number in alphabetical order:
Little
25
Star
35
Twinkle
55 65

Flowchart:

C Programming Flowchart: Read a list of pairs of a word and a page number, and prints the word and a list of the corresponding page numbers.

C programming Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a C program, which adds up columns and rows of specified table.
Next: Write a C program that reads an expression and evaluates it.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



C Programming: Tips of the Day

Static variable inside of a function in C

The scope of variable is where the variable name can be seen. Here, x is visible only inside function foo().

The lifetime of a variable is the period over which it exists. If x were defined without the keyword static, the lifetime would be from the entry into foo() to the return from foo(); so it would be re-initialized to 5 on every call.

The keyword static acts to extend the lifetime of a variable to the lifetime of the programme; e.g. initialization occurs once and once only and then the variable retains its value - whatever it has come to be - over all future calls to foo().

Ref : https://bit.ly/3fOq7XP