Reading sentences in with char array

This is a discussion on Reading sentences in with char array within the C++ Programming forums, part of the General Programming Boards category; I know that i could use a string data type but please don't give that as a solution because strings ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    30

    Reading sentences in with char array

    I know that i could use a string data type but please don't give that as a solution because strings haven't been covered yet in the book. when i print out the array now, only the first word that is entered shows up. i realize that i have the code set to change every letter to upper case but i was just doing that to test. how do i write code to only change the first letter of each word to upper case using char arrays?

    here is code so far: i'm supposed to change the first letter of each word to capital case.

    Code:
    #include <iostream>
    using namespace std;
    
    void upper (char[]);
    
    int main()
    {
    	char proper[21];
    
    	cout << "Enter a word: ";
    	cin >> proper;
    
    	upper(proper);
    
    	return 0;
    }
    
    void upper (char prop[])
    {
    	for (int i = 0; i < 21; i++)
    	{
    		if (prop[i] == 0)
    			break;
    
    		prop[i] -= 32;
    
    		cout << prop[i];
    	}
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> how do i write code to only change the first letter of each word to upper case using char arrays?

    You could use a loop. Your code reads in one word. You need to loop and keep reading in words.

    Or, if you've learned getline, you can use getline to read the entire line (including spaces). If you're only supposed to upper case the first letter of each word, you'd have to add code to figure out where each new word starts.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    30
    how do i figure out where each new word starts using code?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by AJOHNZ View Post
    how do i figure out where each new word starts using code?
    How do you, yourself, figure out where each new word starts?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 07:02 AM
  3. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  4. Reading to array of char *s
    By VanJay011379 in forum C Programming
    Replies: 2
    Last Post: 04-28-2005, 05:45 PM
  5. reading int into 2-d char array
    By lambs4 in forum C Programming
    Replies: 7
    Last Post: 07-24-2002, 05:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21