Advertisement
apl-mhd

Problem 4

Mar 16th, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <ctype.h>
  4. using namespace std;
  5. struct counting{
  6.  
  7.     int count;
  8.     int data;
  9.  
  10.     struct counting *next;
  11.  
  12. };
  13.  
  14. typedef counting node;
  15.  
  16. node  *charterInsert(node *head){
  17.  
  18.         node *temp, *prev ,*temp2;
  19.  
  20.         for(int i = 'a'; i<='z'; i++){
  21.  
  22.  
  23.         if(head == NULL){
  24.             head = new node();
  25.             temp = new node();
  26.  
  27.             temp->data = i;
  28.             temp->count=0;
  29.             temp->next = NULL;
  30.             head->next = temp;
  31.             prev = temp;
  32.  
  33.     }
  34.     else{
  35.  
  36.         temp2 = new node();
  37.         temp2->data =i;
  38.         temp2->count=0;
  39.         temp2->next = NULL;
  40.         prev->next = temp2;
  41.  
  42.         prev = temp2;
  43.  
  44.  
  45.     }
  46.  
  47.  
  48.  
  49. }
  50.  
  51.   return head;
  52.  
  53. }
  54.  
  55. node *charCount(node *head){
  56.    
  57.  
  58.         string str;
  59.         int len;
  60.         node *temp;
  61.  
  62.         cin>>str;
  63.         len = str.size();
  64.      
  65.     for(int i=0; i<len; i++){
  66.        
  67.         //printf("%c",str[i]);
  68.    
  69.         temp =head->next;
  70.            
  71.         while(temp !=NULL){
  72.                
  73.             //printf(" %c ",temp->data);
  74.             if(tolower(str[i]) == temp->data){
  75.             temp->count++;
  76.         }
  77.             temp= temp->next;
  78.         }
  79.        
  80.  
  81.     }
  82.  
  83.  
  84. return head;
  85. }
  86.  
  87.  
  88.  
  89.  
  90. void display(node *head){
  91.     head=head->next;
  92.    
  93.     while(head!=NULL){
  94.  
  95.  
  96.         printf("%c = %d\n",head->data, head->count);
  97.  
  98.         head = head->next;
  99.     }
  100.  
  101. }
  102.  
  103.  
  104. int main()
  105. {
  106.    
  107.    node *head;
  108.  
  109.    head = NULL;
  110.  
  111.  
  112.   head =  charterInsert(head);
  113.  
  114.     head = charCount(head);
  115.  
  116.  
  117.   display(head);
  118.  
  119.     return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement