Convertion

Hello,

I am new this site. It has been about 8 years since I last written any code so I am a bit rusty.

I am trying to write a program in C++ that scans through the line and replaces each of the blank characters with a dash character, then passes it to the thread and converts the first lower case letter to upper case.

Thank you in advance for your help.

Here is what I have so far:

#include <iostream>
#include <cctype>
#include <cstring>

struct convert1 //int covert1;
{
static const char dash = '-' ; //dash character
};

const char convert1::dash ;

int main()
{

const int MAX_LENGTH = 100;

bool dash = true;

for (int i=0;i <convert1.dash(' ');
if (convert1[i]==' ')
convert1[i]='_';
}

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

std::cout << std::endl << convert1 << convert2 << std::endl;
std::cin.get();

return 0;

endl;
}
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
29
30
31
32
33
34
35
36
#include <iostream>
#include <string>
#include <cctype>

int main()
{
    std::cout << "Write a sentence and the first letter of each word will be "
                 "capitalized for you\nand all spaces will be replaced with a dash.\n";

    // http://www.mochima.com/tutorials/strings.html
    std::string sentence ;

    // http://www.cplusplus.com/reference/string/string/getline/
    std::getline( std::cin, sentence ) ;
    std::cout << sentence << '\n' ;

    bool next_char_begins_word = true ;

    // http://www.stroustrup.com/C++11FAQ.html#for
    for( char& c : sentence )
    {
        if( std::isspace(c) )
        {
            c = '-' ;
            next_char_begins_word = true ;
        }

        else if( next_char_begins_word )
        {
            if( std::isalpha(c) ) c = std::toupper(c) ;
            next_char_begins_word = false ;
        }
    }

    std::cout << sentence << '\n' ;
}

http://coliru.stacked-crooked.com/a/fc8dc6adb123df90
Thank you very much for taking the time to respond.
Topic archived. No new replies allowed.