VB icon

Find sub string Count in file.

Email
Submitted on: 8/14/2016 9:12:55 PM
By: Ben128  
Level: Beginner
User Rating: Unrated
Compatibility: C++ (general), Microsoft Visual C++
Views: 1222
 
     This small bit of code shows how to look in a input file and count the number of times a single string is found. The result is then shown to the user., strings can be serached for in text compare or binary set true to TextCompare to locate a word with mixed case.

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Find sub string Count in file.
// Description:This small bit of code shows how to look in a input file and count the number of times a single string is found. The result is then shown to the user., strings can be serached for in text compare or binary set true to TextCompare to locate a word with mixed case.
// By: Ben128
//**************************************

#include <stdio.h>
#include <iostream>
#include <fstream>
using namespace std;
using std::ifstream;
char *LCase(char *s){
	char *ret = new char[strlen(s)];
	strcpy(ret,s);
	for(int i = 0;i<strlen(s);i++){
		ret[i] = tolower(s[i]);
	}
	return ret;
}
int StringCount(char *s, char *substr, bool TextCommpare = false){
	int cnt = 0;
	char *found = nullptr;
	char *pstr = s;
	while(true){
		//Do text compare
		if(TextCommpare){
			found = strstr(LCase(pstr),LCase(substr));
		}
		else{
			//Binary compare
			found = strstr(pstr,substr);
		}
		//Check if found.
		if(!found){
			break;
		}else{
			//INC counter
			cnt++;
			//Look for the next substr
			pstr = found + strlen(substr);
		}
	}
	delete[]found;
	//Return strings found
	return cnt;
}
int main(int argc, char* argv[]){
	int len = 0;
	int wordcnt = 0;
	bool CompareFlag = false;
	char flag = '\0';
	if(argc < 2){
		cout << "Invaild Syntax"
			<< endl << "USAGE: <InputFile><Find String> [CompareMode]" << endl;
		return 0;
	}
	//Check for compare flag
	if(argc == 4){
		if(argv[3][0] == '/'){
			//Set text compare if T is found.
			CompareFlag= toupper(argv[3][1]) == 'T';
		}
	}
	//Try and open the input file.
	ifstream Data(argv[1],ios::binary);
	if(!Data.good()){
		cout << "IO/Error cannot read source file " <<
			endl << argv[1] << endl;
		return 0;
	}
	//Get the full length of the input file.
	Data.seekg(0,Data.end);
	len = Data.tellg();
	Data.seekg(0,Data.beg);
	//Resize char* to hold the file contents.
	char *sPtr = new char[len];
	//Read the contents of the file into sPtr
	Data.read(sPtr,len);
	//Appending ending NULL to string.
	sPtr[len - 1] = '\0';
	//Close the file.
	Data.close();
	//Get number of words found in input file.
	wordcnt = StringCount(sPtr,argv[2],CompareFlag);
	//Print were ever the sub string was found in input file
	if(wordcnt == 0){
		cout << "The string '" << argv[2] << "' was not found." << endl;
	}else if(wordcnt == 1){
		cout << "The string '" << argv[2] << "' was found one time." << endl;
	}else{
		cout << "The string '" << argv[2] << "' was found " << wordcnt << " Times." << endl;
	}
	//Delete the input file contens from memory.
	delete[]sPtr;
	//Return with good exit code.
	return 1;
}


Other 50 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.