Home > Back-end >  String to calculate the number of words
String to calculate the number of words

Time:11-11

Using string stored some English words, such as "FORBES, The CEO of Boloco 's Social Media Strategy,"
Output of all the capital letters of the word, and calculate the number of
Hope to get some code or good ideas

The problem is the number of calculation is not difficult, mainly how to determine the capital letters of the word;
My train of thought:
1. To find the location of the first space in the string (make sure the words)
2. Record the blank space position, and through the loop determine whether the element is capitalized, if all the capital, the output, and the number of + 1; If not for the caps, jump out of the loop;
3. To continue, to find a space position

CodePudding user response:

Make sure of the word, and then traverse the word, judge whether to have lowercase characters,

CodePudding user response:

reference 1/f, confident boy reply:
make sure of the word, and then traverse the word, judge whether to have lowercase characters,
I think the final judgment should be: whether all uppercase characters

CodePudding user response:

I also think we should make sure of the words in the capital, check if the words are all

CodePudding user response:

Simple lexical analyzer, finite state machine, automatically, efficiency is the highest, no one,

 
Public enum MachineState
{
START,
UPPER_WORDS,
}

Public int GetUpperWordsCount (string) p
{
MachineState state=MachineState. START;
int count=0;
The foreach (char c p) in
{
The switch (state)
{
Case MatchineState. START:
If (c & gt;='A' & amp; & c <='Z')
State=MachineState. UPPER_WORDS;
break;
Case MatchineState. UPPER_WORDS:
If (c=='| | c==' \ t '| | c==' \ r '| | c==' \ n '| | c==', '| | c=='. ')
{
+ + count;
State=MatchineState. START;
}
Else if (c>='A' & amp; & c <='Z')
break;
The else
State=MatchineState. START;
break;
}
}
}

CodePudding user response:

Good people to do what, give you perfect code:

 
//state machine three states
Public enum MachineState
{
START,
UPPER_WORDS,
NOTUPPER_WORDS
}

//determine whether a character for a blank
Private bool IsSpace (char c)
{
Return (c=='| | c==' \ t '| | c==' \ r '| | c==' \ n ');
}

//determine whether a character for the word of separator, you can see a circumstance to add other punctuation
Private bool IsSeparator (char c)
{
Return IsSpace (c) | | (c==', '| | c=='. ');
}

//whether all letters to uppercase words number
Public int GetUpperWordsCount (string) p
{
MachineState state=MachineState. START;
int count=0;
The foreach (char c p) in
{
The switch (state)
{
Case MachineState. START:
If (c & gt;='A' & amp; & c <='Z')//if the first character to uppercase, state change
State=MachineState. UPPER_WORDS;
Else if (IsSpace (c))//if it is blank, keep the start state
break;
Other information else//, lowercase state
State=MachineState. NOTUPPER_WORDS;
break;

Case MachineState. UPPER_WORDS:
If (IsSeparator (c))//if there are any words space, count and change of state
{
+ + count;
State=MachineState. START;
}
Else if (c>='A' & amp; & c <='Z')//if capital, or keeping state
break;
Other information else//(word has not uppercase characters), the state of the capital
State=MachineState. NOTUPPER_WORDS;
break;

Case MachineState. NOTUPPER_WORDS:
If (IsSeparator (c))//until the separator, otherwise are capitalized state
{
State=MachineState. START;
}
break;
}
}
}

CodePudding user response:

Lexical analyzer, finite state machine automatically, is the knowledge in compiling principle, its advantage is that multiple whitespace characters in a row, it is easy to handle, will not affect the calculation results, only traversal over the character, the efficiency is very high, and understand the difficult words, draw a state diagram, and is easy to understand,

CodePudding user response:

refer to 6th floor sdhexu response:
lexical analyzer, finite state machine automatically, is the knowledge in compiling principle, its advantage is that multiple whitespace characters in a row, it is easy to handle, will not affect the calculation results, only traversal over the character, the efficiency is very high, and understand the difficult words, draw a state diagram, and is easy to understand,


Efficiency is high, this is my part of the last line of C language exam programming problem in content, tomorrow to have a good look at your code, thank you very much! Also on before compilation principle course, you can see yourself to learn skills do not jing, to work hard!
  • Related