Help with acronym detection/counter function?

M

me

Extreme newbie here

Trying to figure a way to detect an acronym in a
paragraph of text.

Then must count the number of characters in that
acronym.

This would be a function that returns back to main

I'm assuming all acronyms are in CAPS, but how to
detect a word FULL of CAPS?

Then return character count in that word?
 
S

samwisegamgee

First you need isupper() from <cctype> for testing uppercase letters;
next you
need to a string object to hold the entire paragraph. Iterate through
string
from begin to end, testing each letter with isupper() and increment
count if
isupper() tests true.
 
P

P. Lepin

Extreme newbie here

Trying to figure a way to detect an acronym in a
paragraph of text.

Then must count the number of characters in that
acronym.

This would be a function that returns back to main

I'm assuming all acronyms are in CAPS, but how to
detect a word FULL of CAPS?

Then return character count in that word?

Something like this should work:

#include<algorithm>
#include<string>
#include<cctype>
using namespace std;
struct inspector{struct s{bool y_;size_t l_,ml_;
s():y_(true),l_(0),ml_(0){}};
s&s_;inspector(s&s):s_(s){}
void operator()(char c){
if(isalpha(c)){++s_.l_;s_.y_=s_.y_&&isupper(c);return;}
s_.ml_=max(s_.y_?s_.l_:s_.ml_,s_.ml_);
s_.y_=s_.y_||!s_.y_;s_.l_-=s_.l_;}};
size_t acronym(const string&s){
inspector::s r;inspector gadget(r);
for_each(s.begin(),s.end(),gadget);
return r.ml_;}

Or, perhaps a bit more elegantly:

#include<numeric>
#include<utility>
#include<string>
#include<cctype>
using namespace std;
typedef pair<bool,pair<size_t,size_t> >a;
a f(a a,char c){
if(isalpha(c)){++a.second.first;a.first=a.first&&isupper(c);}
else{
a.second.second=
max(a.first?a.second.first:a.second.second,a.second.second);
a.second.first-=a.second.first;a.first=a.first||!a.first;}
return a;}
size_t acronym(const string&s){
a a;a.first=a.first||!a.first;
return accumulate(s.begin(),s.end(),a,f).second.second;}
 
M

Michael Doubez

Something like this should work:

#include<algorithm>
#include<string>
#include<cctype>
using namespace std;
struct inspector{struct s{bool y_;size_t l_,ml_;
    s():y_(true),l_(0),ml_(0){}};
  s&s_;inspector(s&s):s_(s){}
  void operator()(char c){
    if(isalpha(c)){++s_.l_;s_.y_=s_.y_&&isupper(c);return;}
    s_.ml_=max(s_.y_?s_.l_:s_.ml_,s_.ml_);
    s_.y_=s_.y_||!s_.y_;s_.l_-=s_.l_;}};
size_t acronym(const string&s){
  inspector::s r;inspector gadget(r);
  for_each(s.begin(),s.end(),gadget);
  return r.ml_;}

You don't actually need a member as a return value, std::for_each()
gives enough guarantes that you can write:
const inspector result = std::for_each(s.begin(),s.end(),
inspector() );

With inspector::s being a plain member (or no internal struct at all).

[snip]
 
P

P. Lepin

Michael said:
You don't actually need a member as a return value, std::for_each()
gives enough guarantes that you can write:
const inspector result = std::for_each(s.begin(),s.end(),
inspector() );

Ah, but where's the *fun* in that? If I were, in good conscience, trying to
do someone's beginner-level homework for them, I likely wouldn't reach into
<algorithm> at all.

Besides, that way I would miss an opportunity to write "inspector
gadget(r);" in the code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top