Answer to Question #332331 in C++ for Zee

Question #332331

Task 1



Write a C++ function in which it reads namesfrom file “data.txt” into character array and remove



the repeating names. Write your updated names list in another file called “output.txt”



Example:



Data.txt



Hira



Ali



Ahmad



Imran



Ali



Warda



Annie



Ali



Kinza



Hira



Output.txt



Hira



Ali



Ahmad



Imran



Warda



Annie



Kinza

1
Expert's answer
2022-04-23T18:13:58-0400




//After running the program get follow results in "output.txt"






//Hira and Ali was deleted(because those names repeating

//Code-----------

#include <iostream>
#include <string.h>
#include <iomanip>
#include  <fstream>
#define SIZE 6555//how many lines maximum file
using namespace std;
int main()
{
  ifstream inp("data.txt");//Input file handling
  if(!inp)
  {
    cout<<"Sorry file not found \n";
    return 0;
  }
  char **names=(char**)malloc(sizeof(char*)*SIZE);
  for(unsigned i=0;i<SIZE;i++)
    names[i]=(char*)malloc(sizeof(char)*SIZE);
  int sz=0;
  char buf[50];
  while(inp.good())
    {
      inp>>buf;
      strcpy(names[sz],buf);
      sz++;
    }
  
  ofstream out("output.txt",ios::trunc);
  //check
  for(unsigned i=0;i<sz;i++)
    {
      int cnthis=0;
      for(unsigned j=0;j<sz;j++)
        {
          if(strcmp(names[i],names[j])==0)
            cnthis++;//count for repeat
        }
    
  if(cnthis==1)
  {
   out<<names[i]<<"\n"; 
  }
      }
  //Disalloc memory
  for(unsigned i=0;i<SIZE;i++)
     free(names[i]);
  free(names);
  inp.close();
  out.close();
  return 0;
}




Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS