Hey guys,
Its a bit urgent..
please see the code below:
Code:
#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<ctype.h>
int main()
{
int n,i,print_count=0,word_count=0,j,string_length,index,match,different=0;
char *word; //variable to hold the word to be removed.
char current_char,first; //current_char holds the letters in the word_array one_by_one
cout<<endl<<"PLEASE ENTER THE WORD TO BE REMOVED FROM WORD_ARRAY"<<endl;
gets(word);
//cout<<endl<<word;
//cout<<endl<<strlen(word);
string_length=strlen(word); //length of string to be removed
first=word[0]; //first holds first char of word
cout<<endl<<"PLEASE ENTER NUMBER OF WORDS IN WORD_ARRAY"<<endl;
cin>>word_count;
if(word_count<=0) //validates the input
{
cout<<endl<<"PLEASE NOTE THAT WORD COUNT SHOULD BE DIGIT GRETAER THAN ZERO";
}
else
{
char word_array[word_count][200]; //if valid input,dynamically create 2D word array
cout<<endl<<"PLEASE ENTER THE WORDS IN THE WORD_ARRAY ONE BY ONE"<<endl;
for(i=0;i<word_count;i++)
{
cout<<endl<<"ENTER WORD NUMBER "<<i+1<<">>"<<endl; //read the words into the 2D array
gets(word_array[i]);
}
/*for(i=0;i<word_count;i++)
{
cout<<endl<<word_array[i];
}*/
//cout<<endl<<"here it goes"<<endl;
cout<<endl<<endl<<"Output:"<<endl<<endl;
cout<<"["; //square bracket at beginning of output
for(i=0;i<word_count;i++) // loop to go through each word of 2d word_array
{
cout<<"\""; //double quotes before each word in output
for(j=0;word_array[i][j]!='\0';) // loop to check the last character of current word
{
different=0;
current_char=word_array[i][j]; // set current_char
//cout<<endl<<"current char="<<current_char<<endl;
if(tolower(current_char)==tolower(first)) //if there is a first letter match,then only go to letter removal logic
{
//cout<<endl<<"first char match"<<endl;
index=0; // index increments through each letter of 'word' variable. set it to zero,each time ther is a 1st letter match.
match=0;
while(index<=(string_length-1)) // loop till index reaches the last character
{
if(tolower(word[index])==tolower(word_array[i][j])) //if there is a match in letters,go inside if block
{
//cout<<endl<<"hurray";
index++; //in case of match,make index point to next char of word
match++; //if a match,increment no of matched chars.
j++; //in case of match,point to next char of string in word_array
//cout<<endl<<"match="<<match;
}
else
break; //if no match,get out of loop
}
if(match!=string_length) //if the entire word does not match,move the j index back to the first letter of partially matched word
{
//cout<<endl<<endl<<"j="<<j;
different=1;
j=j-match;
}
}
/***
A 'different' variable is used here so that even when the word to be removed appears continiously
in the input array,it will be removed by the code below.At the same time,it will also print partially
matched words.
***/
//(tolower (word_array[i][j]))!=tolower(first) checks if word which immediately follows the previous word also has to be removed.
//The condition (different==1) ensures that partially matched words will not be skipped due to the condition tolower (word_array[i][j]))!=tolower(first)
if(word_array[i][j]!='\0'&& ((tolower (word_array[i][j]))!=tolower(first) || (different==1)))
{
//cout<<endl<<"not null";
cout<<word_array[i][j]; //if not a part of given word,print onto screen
j++; //increment and check next char
}
}
cout<<"\""; //double quotes before each word in output
//cout<<print_count++;
if(i<word_count-1)
cout<<",";
}
cout<<"]"; //square bracket at end of output
}
}
this code compiles and run in the c- Free 4.0 compiler but in the online comipler ideone, it was giving a compilation error . so i googled about it and changed iostream.h to iostream and added using namespace std;
but now its giving a runtime error. is it actually possible to run this program online?