How do you input words into a char array? I thought i can use getline but when I
ID: 3530084 • Letter: H
Question
How do you input words into a char array? I thought i can use getline but when I cout<<Array[0]; I still get one letter. Am I missing something? I need it to be, for example, input text is: " This is the text " and the program will output Array[0] as "This", not "T" My other question is the switch statement. How do I determine the character classification functions of the array? Also, would it work if my array is one whole word instead of letters? I have a switch statement, but that does not seem to help? while(Token[i]!='') { switch (Token[i]) { case (islower(Token[i])): case (isupper(Token[i])): WORD(Token); break; case (isdigit(Token[i])): DIGIT(Token); break; case (ispunct(Token[i])): PUNCT(Token); break; } i++; } I feel that my question is getting broader and broader... Here is my assignment if you need it for reference: Create a program that inputs from the user a line of text. This line will be put into an array. The program will output the words one by one with the corresponding character classification. For example: User input: "My name is Sam! I am 19!" program output: My [word] name [word] is [word] Sam [word] ! [punctuation] I [word] am [word] 19 [number] ! [punctuation] My thought is to input the line letter by letter into array A then use array B to store one word at a time then determine the array B by a switch statement to see if it is lower, upper, or digit and output it I hope that makes sense, I am very new to this, therefore any help, criticism, or even praise is welcome. Thanks in advanceExplanation / Answer
You are getting one letter because doing array[0] just prints out the character at index 0. If you want to print out the entire character array, you have to do cout << array. If you want to read it into an array, you can do: cin >> array. The arrow operator will automatically read in up to the first white space character (like a space or new line) and then add a null character onto the end. Doing cout << array will automatically read out the entire array until it hits the terminal character.
It looks like maybe you want to read in the whole line at once, including spaces, though. In that case you probably want to use the get() function. It is used like this:
cin.get(char*, streamSize, char delimiter).
So if you want to get at most 100 characters into a character array called charArray, and you want to read until you hit a new line, do this:
cin.get(charArray, 100, '/n');
This function will also automatically add a null character onto the end of whatever it reads in.
Alternatively, you could do an array of arrays, and just put a whole word into each array. Since you aren't just separating by white space, though, I don't think that would make your life easier. I would just put it all into one character array and loop through each character. You can then do some checks on the individual characters(assuming you are checking a char called ch):
if (isalpha(ch)) will check if a character is a letter, regardless of case
if(ch >= '0' && ch <= '9') will check if it's a number. Make sure to keep the apostrophe marks to denote 0 and 9 as characters instead of numbers!
if (ch == '!" || ch == '.' || ch == '?') will check if it's one of the three basic punctuation marks. You may need to add more, depending on what all qualifies as punctuation for your assignment.
You'll probably also want to check if it's a space, so that you can just print that out. You can then throw all those checks inside of a while loop that loops through your entire array, checking for the null character ( '' ) to see when you should stop.
If you want to print out a whole word, you can do:
if (isalpha(ch)) {
while (isalpha ch) {
//print out ch
//Set ch to be the next character in the array
}
//print out "[word]"
}
So you check if the first character in the word is a letter, then you keep printing characters out until you hit a non-letter. This would presumably be the end of your word (whether it is a space or punctuation mark or something else entirely), and then you print out that it is a word. You can do something similar for numbers and punctuation.
Just remember that when you have an array, say array[100], that you can access that in two ways:
array means the ENTIRE array, not just one element
array[#] means ONE element in that array. In this case, that would be a single character, and you should treat it exactly like you would if you were referring to a single char variable.
Hope that helps!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.