Write a program to capitalize the 1st char of each word in a sentence

Feb 24, 2012 at 5:06pm
eg"Return An Integer That Is The Start Position Of The First Occurrence Of The String"
Last edited on Feb 24, 2012 at 5:08pm
Feb 24, 2012 at 5:13pm
int main () {
bool doItYourself = true;
while (doItYourself) { cout<<"i'm not your slave"<<endl; }
return EXIT_SUCCESS;
}
Feb 24, 2012 at 5:16pm
cout << implode(transform(explode(str,' '),capitalizeFirstChar),' ') << endl;
You might have to implement some of those functions yourself.
Feb 24, 2012 at 5:20pm
ok thanks will put my code n a min
Feb 24, 2012 at 5:55pm
#include <iostream>
#include <string>


using namespace std;
int main ()
int tolower (int c);
int toupper (int c);
{
char a,b,c;
cin >> a;
b = toupper (a);
c = tolower (a);

char str 2 [100];
cout << "Please type a sentence \n";
cin >> getline (str2,100);


system("pause");
return 0;
}
Feb 24, 2012 at 6:40pm
#include <iostream>
#include <string>


using namespace std;
//int main ()
int tolower (int c);
int toupper (int c);
{
char a,b,c;
cin >> a;
b = toupper (a);
c = tolower (a);

char str 2 [100];
cout << "Please type a sentence \n";
cin >> getline (str2,100);
cout << "capitalize firt character in each sentence";
cin >> Please type a sentence ;
system("pause");
return 0;
}


stil cant get it too work
Feb 24, 2012 at 6:51pm
You are doing it all wrong!

int main (){
cout << "Please type a sentence \n";
cin>>the cat is blue
CAPITALIZE!
cout<<sentence_capitalized
}

this should work + -
Feb 24, 2012 at 7:10pm
#include <iostream>
#include <string>


using namespace std;
int main ()
{
char 1st character;
cout << "Please type a sentence \n";
cin >> the cat is blue;

cout<<sentence_capitalized 1st char;




system("pause");
return 0;
}

I cant get it,im gett'n very sick of c++
Feb 24, 2012 at 8:40pm
Dude, timmmyyy was showing you pseudo-code. It's just an example of the program structure. Where he wrote "CAPITALIZE!" YOU are supposed to put in code that does the actual work. C++ is not magic!
Feb 24, 2012 at 9:39pm
Well sro i still dont know what to do with it,
Feb 24, 2012 at 10:04pm
Do you know how to read a string of text from the user?
Do you know how to write a for loop?
Do you know how to convert a single character to upper case?
Do you know how to access individual characters in a string?


If the answer to any of those questions is "no", then you need to spend some time learning and understanding the basic concepts (That means you need to take some time out to read your notes/book(s) and tutorials, and you need to try writing simpler programs which you can understand).

You won't learn by stabbing in the dark when you're already missing some of the basics, so save yourself the wasted time and frustration by making sure you understand the basics first.
Feb 24, 2012 at 10:17pm
From your earlier code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>

//This function accepts any string and Capitolizes the first character
string Capitolize(string input)
{
    if (input[0] <= 'z' && output[0] >= 'a')
        input[0] -= 0x20; //Convert to upper, ref: http://www.asciitable.com/

    return input;
}

using namespace std;
int main ()
{
    string Sentence;
    cout << "Please type a sentence \n"; 
    getline(cin,Sentence); // Using getline so we can include whitespaces

    cout<<Capitolize(Sentence); // cout the result of a capitolized sentence

    system("pause"); 
    return 0;
}
Last edited on Feb 24, 2012 at 10:18pm
Feb 27, 2012 at 6:13pm
Great code thanks,but it still won't work,capitalize undecleard and no string name coming up in the errors,it won't compile.
Feb 27, 2012 at 6:54pm
Read your textbook.
Feb 27, 2012 at 7:09pm
I let the text book in college and must hand this in first thing in the morning ok.
Feb 27, 2012 at 9:07pm
Any one out there that can give me a hand please
Feb 27, 2012 at 10:19pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <cctype>
#include <cstring>

const int MAX_LENGTH = 500;

int main()
{
    char sentence[MAX_LENGTH];
    bool cap_it = true;
    std::cout << "Write a sentence and the first letter of each word will be capitalized for you.\n";
    std::cin.getline(sentence, MAX_LENGTH);
    for (int i = 0; i < strlen(sentence); i++)
    {
        if (isalpha(sentence[i]) && cap_it == true)
        {
            sentence[i] = toupper(sentence[i]);
            cap_it = false;
        }
        else
            if (isspace (sentence[i]))
                cap_it = true;
    }

    std::cout << std::endl << sentence << std::endl;

    return 0;
}


Feb 27, 2012 at 10:54pm
std::cin.get(); // add this code right below the cout statement if you need it to freeze the screen when program terminates.
Feb 27, 2012 at 11:14pm

How exactly do you think you're helping anybody by spoon-feeding somebody a solution to their homework? The point of these forums is to assist beginners in learning - it's not helpful at all to hand out code to someone who either lacks understanding (And therefore isn't going to get that understanding from just copying a bit of code from the internet) or is plainly too lazy to spend their own time studying and practising.

In fact it's entirely counter-productive; not least because it encourages posters to keep on "begging" others to do their work for them without making the effort to learn anything themselves, and because the next time the poster comes across a more difficult problem which they can't solve, they'll be even more stuck since they've already got gaps in their understanding from when they missed out the last time. Please don't do it again.
Last edited on Feb 27, 2012 at 11:18pm
Topic archived. No new replies allowed.