Industrial Training

TURBO C++



////////////////////////////////TURBO C++//////////////////////////////////
/****************************************************************************
1..Wriet a program in C++ to find the number of occurrencesof each word on
input stream. Read the words from the standard input stream into the string
'Buf',this word is searched in an array of structure 'Pair' [which contains
two fields :string, a 'value' field indicating the number of occurrences of
the string].If the word is found in the array, corresponding 'value' field is
increased, otherwise word is inserted into the array setting the 'value'
field to 0.Also write the program using the 'map'(implemented in version 2)
library function.
****************************************************************************/
#include<iostream.h>
#include<string.h>
#include<conio.h>
static int counter=0;
static int over=0; //flag to indicate end of input
char input[500]; //holds string from the stream
char buf[25]; //buffer
struct pair
{
char string[20];
int value;
}p[200];
void main()
{

clrscr();
void get_word(); //function prototypes
int search();
void add();
void display();
p[0].value=-1; //used to indicate the last element
//of the array that contains a valid
//element
cout<<"Enter a string(complete with #)"
<<endl;
cin.getline(input,500,'#'); //getting the string into input string
while(1) //while input contains more words
{
get_word(); //get indivisual word
if(over)
break;
if(!search()) //search for previous presence
add(); //word not present so add it to the array
}
display();
getche();
}
/***************************************************************************
this function gets indivisual words from the input
*****************************************************************************/
void get_word()
{
int x=0;
char temp;
temp=input[counter];
if(temp=='\0')
{
over=1;
return;
}
while(temp!=' '&&temp!='\n'&&temp!=';'&&temp!=','&&temp!='.'&&temp!='\0')
{
buf[x++]=temp;
temp=input[++counter];
}
counter++;
buf[x++]='\0';
}
/***************************************************************************
this function searches for previous presence of the present word in the 'buf'
if present it returns 1 else it returns 0
***************************************************************************/
int search()
{
int count=0;
while(p[count].value!=-1)
{
if(strcmp(p[count].string,buf)==0)
{
p[count].value++;
return(1);
}
count++;
}
return(0);
}
/**************************************************************************
this function adds a new word in the array of structures
**************************************************************************/
void add()
{
int count=0;
while(p[count].value!=-1)
count++;
strcpy(p[count].string,buf);
p[count].value=0;
p[++count].value=-1;
return;
}
/*************************************************************************
the function displays the final result
*************************************************************************/
void display()
{
int count=0;
while(p[count].value!=-1)
{
cout<<endl
<<p[count].value
<<" "
<<p[count].string;
count++;
}
}
/***************************************************************************/

Hi I am Pluto.