Click to See Complete Forum and Search --> : Detecting String as specified


xiaolim
July 29th, 2008, 06:25 AM
hi, i am still new to c++, as well as in this forum, my apologies to those who are in the visual c++ side, i should have posted my question. i am sorry.
so here is my code,


#include <iostream>
#include <cstring>
#include <fstream>
#include <conio.h>

using namespace std;


int main(void)
{
int total=0;
int upper=0;
int lower=0;
int digits=0;
int blanks=0;
int eosp=0;
int others=0;

char buf[80];
ifstream fin;
string fname;

cout << "Welcome\n" << endl;
cout << "This programe will analyze the file content &" << endl;
cout << "compute the statistics of the file you input.\n\n\n\n\n" << endl;
system("pause");
system("cls");

do
{
cout << "Enter input data file name:\n";
cin >> fname; // Get the file name.
fin.open(fname.c_str()); // Convert to C-string and open it.
if (!fin)
{ // Will fail if didn't exist.
cout << "Unable to open " << filename << endl;
cin.get();
system("cls");
}
} while(!fin);


cout << "Total number of characters: " << total << endl;
cout << "Number of uppercase characters: " << upper << endl;
cout << "Number of lowercase characters: " << lower <<endl;
cout << "Number of decimal digits: " << digits << endl;
cout << "Number of blanks: " << blanks << endl;
cout << "Number of end-of-sentence punctuation marks: "<< eosp << endl;
cout << "Others: " << others << endl;
cout << "" << endl;


system("pause");

return 0;
}



i wanted it to detect the uppercase, lowercase,digits,blanks, punctuations and others such as spaces. I've declare all the variables and assign them to initial 0.so after the file is open, the program should be able to read all the lines in the file and detects and then display them out as accordingly. any guide or help? an example will be more then enough. thank you very much

ch0co
July 29th, 2008, 07:13 AM
Can you state what the problem is exactly, or what you want done.
i.e "My snippet doesn't compile, i get error messages.

I'd advise you to learn STL if you've not already started which I guess you haven't because it should be covered in the early chapters of the book.

for example to detect spaces in your file.. modify this sample snippet to your taste. I'm a bit lazy so I can help you modify your code I'd just show you how to do it also if you want to replace.


string removeSpaces(string s)
{
// find the offset of the first space
// keep searching the string until no more spaces found
size_t offset;
while((offset = s.find(" ")) != -1)
{
// remove the space just discovered
s.erase(offset, 1);
}
return s;
}

// insertPhrase - insert a phrase in the position of
// <ip> for insertion point
string insertPhrase(string source)
{
size_t offset = source.find("<ip>");
if (offset != -1)
{
source.erase(offset, 3);
source.insert(offset, "Richard");
}
return source;
}


In the while loop of the find spaces you can just declare a local variable in the function and place it in the loop to store the total number of spaces it has found. i.e

count++


Regards,
Richard Aberefa.

GNiewerth
July 29th, 2008, 08:28 AM
You need to iterate over all characters of the input string and check the current character.


unsigned int uiPunctuation = 0;
unsigned int uiDigit = 0;
unsigned int uiWhiteSpace = 0;

for( std::string::iterator it = Input.begin(); it != Input.end(); ++it )
{
// punctuation?
if( ispunct( *it ) )
{
uiPunctuation++;
}
else if( isdigit( *it ) )
{
uiDigit++;
}
else if( isspace( *it ) )
{
uiWhiteSpace++;
}
// more to check
}


@ch0co
remove_spaces can be efficiently implemented by


s.erase( remove( s.begin(), s.end(), ' ' ) );