Search Words Alphabetically in a String with a Character Comparison

The Program search words in a string alphabetically with comparing a string. The Program reads text from a text file and then start comparing them alphabetically. First pick a and find all the words start with a and continue it until last z or last words of the text file occur.

Source Code 

LinkList.h

#include <cstdlib>
# include <iostream>
# include <string>
# include <fstream>
using namespace std;
class linked_list 
{ 
public: 
  typedef std::string value_type; 
 
// here comes other functions of ADT  
// CONSTRUCTOR 
linked_list( const value_type& init_data = value_type( ), 
      linked_list* init_link = NULL) 
{ dataa = init_data; linkd = init_link; } 
 
// Member functions to set the data and link fields: 
//Getter and setter for data members 
 void set_data(const value_type& new_data)  
{ dataa = new_data; } 
 
// Constant member function to retrieve the current data: 
  value_type data( ) const  
                 { return dataa; } 
 
//getter and setter for link member 
 void set_link(linked_list* new_link)              
          { linkd = new_link; } 
  const linked_list* link( ) const  
            { return linkd; } 
      linked_list* link( )            
            { return linkd;} 
private: 
     value_type dataa; 
     linked_list* linkd; 
}; 

//Non Member functions for performing operations 
void list_head_insert( linked_list*& head_ptr, const linked_list::value_type& entry); 
void list_end_insert(linked_list*& head_ptr,const linked_list::value_type& entry); 
int count(const linked_list* head_ptr); 
void display_list( const  linked_list* head_ptr);
double sum_list(linked_list* head_ptr);
linked_list* list_search(linked_list* head_ptr, const linked_list::value_type& target); 
const linked_list* list_search (const linked_list* head_ptr, const linked_list::value_type& target); 
linked_list* list_locate(linked_list* head_ptr, std::size_t position); 
const linked_list* list_locate(const linked_list* head_ptr, std::size_t position); 
void list_head_remove(linked_list*& head_ptr); 
void list_remove(linked_list* previous_ptr); 
void list_clear(linked_list*& head_ptr); 
void list_copy(const linked_list* source_ptr, linked_list*& head_ptr, linked_list*& tail_ptr);

Linklist.cpp

#include "linked_list.h" 
void  list_head_insert(  linked_list*&  head_ptr, 
const linked_list::value_type& entry) 
{ 
    head_ptr = new linked_list(entry , head_ptr);
 } 
void search(linked_list*& head_ptr,const linked_list::value_type& target){
 linked_list *node; 
 node=head_ptr;
 cout<<"error";
 if (node==NULL){
  return;
 }else{
  while((node!=NULL)&&(node->data()!=target))
  {
   node=node->link();
  }
  cout<<"DataFount:"<<node->data()<<endl;
 }
} 
void insert_after(linked_list*& head_ptr,const linked_list::value_type& entry,const linked_list::value_type& target){
 cout<<"Inserting "<<entry<<" after "<<target<<"\n";
 linked_list *node,*new_node; 
 new_node=new linked_list(entry);
 node=head_ptr;
 while((node!=NULL)&&(node->data()!=target))
 {
  node=node->link();
 }
 
 new_node->set_link(node->link());
 node->set_link(new_node);
}
void insert_before(linked_list*& head_ptr,const linked_list::value_type& entry,const linked_list::value_type& target){
 cout<<"Inserting "<<entry<<" before "<<target<<"\n";
 linked_list*node_ptr;
 linked_list*new_node;
 linked_list* new_node1;
 new_node1=new linked_list(entry);
 node_ptr = head_ptr;
 
 while((node_ptr!= NULL) &&( node_ptr->data() != target))
 {
  new_node = node_ptr;
  node_ptr = node_ptr->link();
 }
 insert_after(head_ptr,entry,new_node->data());
}
void  list_end_insert(linked_list*&  head_ptr,const 
linked_list::value_type& entry) 
{ 
 linked_list *node; 
 
 linked_list*insert_ptr=NULL; 
 node=head_ptr; 
    if(node==NULL){
     list_head_insert(node,entry);
    }else{
  while(node->link()!=NULL) 
  {      
       node=node->link();
  } 
  insert_ptr= new linked_list(entry); 
  node->set_link(insert_ptr);
 }   
}
void display_list( const  linked_list* head_ptr) 
{ 
const  linked_list *node; 
     node=head_ptr;
     if(node==NULL){
      return;
  }else{
      while(node!=NULL) 
      {
       if(node->data()!=" "){
              cout<<node->data()<<endl; 
              node=node->link();
   }
      }
 }
 } 
 
int count(const linked_list* head_ptr) 
{ 
    const  linked_list *node; 
     node=head_ptr; 
     int counter=0; 
     while(node!=NULL) 
     { 
             node=node->link(); 
             counter=counter+1;    
     } 
    return counter; 
} /*
 double sum_list(linked_list* head_ptr)
 {
  double f=0;
  const  linked_list *node; 
     node=head_ptr; 
     while(node!=NULL) 
     { 
             f=f+node->data();
    node= node->link();
    //f=f+node->data();    
     }                 
    return f;
  
 }*/
 void delete_link(linked_list*&head_ptr ,linked_list::value_type& entry )
{
 linked_list*node_ptr;
 linked_list*new_node;
 
 node_ptr = head_ptr;
 
 while(node_ptr!= NULL && node_ptr->data() != entry)
 {
  new_node = node_ptr;
  node_ptr = node_ptr->link();
 }
 
 new_node->set_link(node_ptr->link());
 cout<< endl<<entry<<"Deleted"<<endl<<endl; 
}
void list_copy(linked_list*& source_ptr , linked_list*& head_ptr){
 cout<<"\n"<<"copying"<<"\n";
 linked_list*temp_head,*temp_source;
 temp_head=head_ptr;
 temp_source=source_ptr;
 while (temp_head!=NULL){
  if(temp_source == NULL){
   temp_source=new linked_list(temp_head->data( ));
   //list_head_insert(temp_source,temp_head->data());
  }else{
   list_end_insert(temp_source,temp_head->data());   
  }
  temp_head=temp_head->link();
  source_ptr=temp_source;
 }
 cout<<"Coppied Successfull"<<endl;
}

main.cpp

#include "linked_list.cpp" 
linked_list** create_array_of_LL(linked_list* head[],int size,string variable);

int main(){
 linked_list* head[26];
 linked_list** return_array;
 
 for(int l=0;l<26;l++){
  head[l]=NULL;
 }
 ofstream myfile;
 myfile.open("example.txt");
 myfile << "Aa Bhe Chuko Dost aa bhe chuko dost Romio khan no";
 myfile << " the quick brown fox jumps over a lazy dog yellow";
 myfile << " while Making deep excavations we found some zonbey";
 myfile << " quaint bronze jewelry Osaka Red pen Some time";
 myfile << " the Fix Boxing wizards jump quickly usman very";
 myfile << " pack my box with five dozen Liquor jugs x=variable";
 myfile << " Extra good helth is Important King Not opp Zorrow ";
 myfile.close();
 
 string variable;
 ifstream myReadFile;
 myReadFile.open("example.txt");
 if (myReadFile.is_open()) {
  int count=0;
  while (!myReadFile.eof()) {
   getline(myReadFile,variable,' ');
   return_array=create_array_of_LL(head,26,variable);
  }
 }
 myReadFile.close();
 for(int i=0;i<26;i++){
  display_list(return_array[i]);
  cout<<"\n";
 }
 
 return 0;
}
linked_list** create_array_of_LL(linked_list* head[] ,int size,string variable){
 linked_list** array= head;
 char varFirstChar;
 int count=0;
 int C_alph=0;
 int S_alph=0;
 
 varFirstChar=variable[count];
 for(int i=65;i<123;i++){
  if((i>=65&&i<=90)){
    if ((int)varFirstChar==i){//(int)varFirstChar gets the ASCII of variable[0]
    list_head_insert(array[C_alph],variable);
    C_alph++;
   }else{
    C_alph++;
   }
  }else if(i>=97&&i<=122){
   if ((int)varFirstChar==i){//(int)varFirstChar gets the ASCII of variable[0]
    list_head_insert(array[S_alph],variable);
    S_alph++;
   }else{
    S_alph++;
   }
  }
 }
 return array;
}

Output of the Program

Search words alphabetically in a string with a character comparison

Share on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment