Thread: Reading sentences in with char array

  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,366
    >> 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,336
    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, 01:01 AM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06: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 int into 2-d char array
    By lambs4 in forum C Programming
    Replies: 7
    Last Post: 07-24-2002, 04:25 PM