1.4 Exercise 1: Need to confirm the definition of character, word and line

XIYU LI

Member
C++ Student
hello, i have successfully composed most of my code and it runs well. But i still want to confirm the definition of character, word and line since i found different explanations in threads related to section 1.4 ex 1 in this forums.
# 1: for example, if a string looks like " a bb c\n\t^D" (there is one space before a,bb,c respectively). Should the number of characters be 4, which means spaces, \t and \n are not characters?
# 2: for example, if a string looks like " a bb c\n\t^D" (there is one space before a,bb,c respectively). Should the number of words be 3?
# 3: for example, if a string entered looks like " a bb c\n\t\naaa^D" (there is one space before a,bb,c respectively). Should the number of lines be 2, which means we ignore lines where no words exist?
I really appreciate your help! :)
@APalley
 
Last edited:

XIYU LI

Member
C++ Student
Everything is a character besides ctrlD. Any whitespace preceded by non-whitespace isa word. Any line with any character (besides just ctrlD) counts as a line.
thank you for the reply!
This might seem annoying but I still want to ask "\t b^D" (there is a space between \t and b) has only one word? Or put it in another way, words are seperated from each other by spaces and \n. So in order to count words, we just simply recognize spaces and \n?
 

APalley

Well-Known Member
thank you for the reply!
This might seem annoying but I still want to ask "\t b^D" (there is a space between \t and b) has only one word? Or put it in another way, words are seperated from each other by spaces and \n. So in order to count words, we just simply recognize spaces and \n?
Yes. Any whitespace
 

XIYU LI

Member
C++ Student
Yes. Any whitespace
Sorry to bother you again.:cry:
I tried to understand your definition of characters, words and lines. but it seems to contradict with other similar thread answered by another TA. Here is the link: 1.4 - 1.4.1 How to count lines.
Everything is a character besides ctrlD.
it means characters include: whitespace, \n, \t, ctrl+D & letters? 2 consecutive whitespaces count as 2 charaters? Multiple repetition of letters, such as aa will be counted as 2 characters?
Any line with any character (besides just ctrlD) counts as a line.
in this way
\n\n^D
has only one line (no whitespaces between \n)?
Any whitespace preceded by non-whitespace is a word.
\n \n^D
(there is a whitespace between \n and \n^D)
has 2 words? since the only whitespace shown here is preceded by \n, which is not a whitespace. and the \n^D is also non-whitespace.

These rules really confuse me because in 1.4.1 the way we count words, lines and characters is not the same with how our eyes conceive in normal daily life.
 

APalley

Well-Known Member
I tried to understand your definition of characters, words and lines. but it seems to contradict with other similar thread answered by another TA. Here is the link: 1.4 - 1.4.1 How to count lines.
I don't see any contradiction. What are you referring to?
it means characters include: whitespace, \n, \t, ctrl+D & letters? 2 consecutive whitespaces count as 2 charaters? Multiple repetition of letters, such as aa will be counted as 2 characters?
in this way
\n\n^D
has only one line (no whitespaces between \n)?
No, this is two lines.
These rules really confuse me because in 1.4.1 the way we count words, lines and characters is not the same with how our eyes conceive in normal daily life.
It is exactly how you would do so in normal daily life. To reiterate:
  • Any character is a character.
  • A line is a line on the screen (which happens to correspond to the number of \n). The only exception is the last line, if it only contains ctrlD and nothing else.
  • A word is something separated on both sides by a whitespace (or, any whitespace preceded by non-whitespace).
 

XIYU LI

Member
C++ Student
I don't see any contradiction. What are you referring to?

No, this is two lines.

It is exactly how you would do so in normal daily life. To reiterate:
  • Any character is a character.
  • A line is a line on the screen (which happens to correspond to the number of \n). The only exception is the last line, if it only contains ctrlD and nothing else.
  • A word is something separated on both sides by a whitespace (or, any whitespace preceded by non-whitespace).
thank you for the reply. it helps!

besides, i am little confused by the definition of words. If the input looks like:
aaa<enter>bbb^D
i thinks there are two words, which are aaa, bbb, even though bbb is not enclosed by blank on its right side. Correct me if I am wrong.
 

XIYU LI

Member
C++ Student
Here is one example of my code, i inputed: a<whitespace>b<whitespace><tab><\n>fjj<tab><tab>a^D
TIM图片20181106140958.png

hope the result of my code is right
 

APalley

Well-Known Member
thank you for the reply. it helps!

besides, i am little confused by the definition of words. If the input looks like:
aaa<enter>bbb^D
i thinks there are two words, which are aaa, bbb, even though bbb is not enclosed by blank on its right side. Correct me if I am wrong.
Indeed, it's two words
 

XIYU LI

Member
C++ Student
xxx.png
yyy.png

Hi, i thought i had overcome the definition problem. However, I found that there are indeed contraction i mentioned in the #5 post on this page.
the first screen shot comes from your reply to others. The second comes from 1.4 - 1.4.1 How to count lines.
Following your opinion, the answers to the 4 questions listed in the second picture should be 1,2,5,4 lines respectively since ^D on an empty line should also count as a line. However the answer in the second picture is 0,1,4,4. @APalley
If you confirm the way in the first picture, then I will go finalize the criteria for lines in my code.
 

XIYU LI

Member
C++ Student
Following the #10 post in this thread. I want to confirm this example for word counting.
aaa<enter>bb<\n>^D
has 3 words? Thank you sooooo much! :) @APalley
 

XIYU LI

Member
C++ Student
Two words :)
when we count lines, we consider ^D as a character. So the empty line with only ^D is also counted as a REAL line.
why when we count words, ^D is not counted as a word given there is a blank (\n) before ^D ?
aaa<enter>bb<enter>^D
2 words, 3 lines?
aaa<enter>bb<1 whitespace>^D
2 words, 2 lines?
 

APalley

Well-Known Member
when we count lines, we consider ^D as a character. So the empty line with only ^D is also counted as a REAL line.
why when we count words, ^D is not counted as a word given there is a blank (\n) before ^D ?
aaa<enter>bb<enter>^D
2 words, 3 lines?
aaa<enter>bb<1 whitespace>^D
2 words, 2 lines?
You are over thinking it :cool: A word is a word. CtrlD is not a word. A line with ctrlD only is still a line; either counting it as a line or not doesn't matter. Either way is ok
 

XIYU LI

Member
C++ Student
You are over thinking it :cool: A word is a word. CtrlD is not a word. A line with ctrlD only is still a line; either counting it as a line or not doesn't matter. Either way is ok
thank your soooo muuuuuch! :giggle:
 

faye.w

Member
C++ Student
  • A word is something separated on both sides by a whitespace (or, any whitespace preceded by non-whitespace).
Hi APalley,

Does this rule apply to <enter> as well? How about the case for "sdf<space>a<\n><space>^D" "sdf<space>a<\n><enter>^D"? Here we have space/enter preceded by non-whitespace "n", but it actually counts <\n> as a word? So should we check two previous characters? Thanks!
 

faye.w

Member
C++ Student
The rule applies in all cases, for any whitespace character
Sorry to bother you again. I read in other threads that "\n" should not be counted as a word, but if it is followed by a whitespace or a enter, it should be treated as a word?
 

APalley

Well-Known Member
Sorry to bother you again. I read in other threads that "\n" should not be counted as a word, but if it is followed by a whitespace or a enter, it should be treated as a word?
A word is a string of characters separated by whitespace. \n alone is never a word
 
Top