Hello all,

I am a noob to C++ and would appreciate any help that I can get. I am taking a beginner's course and got this assignment:

I have to take a users input (i.e. a sentence) and capitalize the first letter of the sentence and make sure each letter there after is lowercase.

hElLo wORld!! -----> Hello world!!

or even

hElLo wORld!! HOw Are yOU? -------> Hello world!! How are you?

This is my code thus far (which capitalizes everything):

#include <iostream>
#include <string.h>
#include <ctype.h>
using namespace std;

//declare the functions used to convert the letters

void convert_to_upper(char *s);

int main()
{
    char s[100];

    //get input from the user

    cout << "Please enter the sentence to be converted and press ENTER: " << endl;
    cin.getline(s, 99);

    //call the function to convert the users input

    convert_to_upper(s);

    //output the corrected sentence

    cout << "Here is the corrected sentence: " << endl;
    cout << s;

    return 0;
}


//function to convert letters into lower case

void convert_to_upper(char *s)
{
    int i;
    int length = strlen(s);

    for (i = 0; i < length; i++)
        s[i] = toupper(s[i]);
}

Any and all help would be greatly appreciated? Thanks again.

Recommended Answers

All 4 Replies

use tolower() instead of toupper() in that loop, then just use touper() on the first letter of the first word.

use tolower() instead of toupper() in that loop, then just use touper() on the first letter of the first word.

I've done that but its only capitalizing the first letter of the first sentence. What if more than one sentence is entered (i.e. heLLO You!!! hOw are YOU?) . I would need to convert that to - Hello you!! How are you?

Here is the addition I have made to the code:

#include <iostream>
#include <string.h>
#include <ctype.h>
using namespace std;

//declare the functions used to convert the letters

void convert_to_lower(char *s);
void capitalize(char *s);

int main()
{
    char s[100];

    //get input from the user

    cout << "Please enter the sentence to be converted and press ENTER: " << endl;
    cin.getline(s, 99);

    //call the function to convert the users input

    convert_to_lower(s);
    capitalize(s);

    //output the corrected sentence

    cout << "Here is the corrected sentence: " << endl;
    cout << s;

    return 0;
}


//function to convert letters into lower case

void convert_to_lower(char *s)
{
    int i;
    int length = strlen(s);

    for (i = 0; i < length; i++)
        s[i] = tolower(s[i]);
}

void capitalize(char *s)
{
        s[0] = toupper(s[0]);
}

If the text contains more than one sentence, first locate the end of the first sentence by locating the normal end-of-sentence terminators (period, question mark, exclamation mark). Then the first word following that is the start of the next sentence.

strpbrk() function will help locate the end-of-sentence.

char *end_of_sentence = strchr(textline, ".?!");

If the text contains more than one sentence, first locate the end of the first sentence by locating the normal end-of-sentence terminators (period, question mark, exclamation mark). Then the first word following that is the start of the next sentence.

strpbrk() function will help locate the end-of-sentence.

char *end_of_sentence = strchr(textline, ".?!");

I was unaware of those so I did further research on them on the internet. My new question is:

does this go into a whole new function and where you have textline, thats where I reference the user input correct?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.