My assignment says I'm supposed to read in an address all on one line separated by pound signs (eg jane doe # p.o. box 123 # new york, new york 97229 #) and output it with correct capitalization and in proper address format like:

Jane Doe
P.O. Box 123
New York, New York 97229

Also we have to use character arrays, no strings, and cannot use global variables. I've gotten it to output in address format, but I have no idea how to capitalize all the letters that need to be uppercase. This is what I have so far:

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

using namespace std;

void read(char word[]);

int main ()
{
           cout << "Enter address: \n";
           char word[40];

           do {
                   read(word);
            } while(strcmp(word, "\n") != 0);

            return 0;
}

void read(char word[])
{
            cin >> word;
            if(strcmp(word, "#") != 0)
                     cout << word << " ";
            else
                     cout << endl;
}

Dani AI

Generated

A few practical points to get this working reliably with C-style arrays and no globals.

First: reading with the extraction operator stops at whitespace, so you won't get the full address line that contains spaces. Read the entire input line into a char buffer and split on #. was right that the first letter of a word should be uppercased, but that alone misses letters after punctuation (for example p.o.). 's idea to replace # with newlines and uppercase after spaces/periods is on the right track — below is a compact, local-only routine that (1) trims each #-separated field, (2) lowercases everything, then (3) uppercases any alphabetic that starts the field or follows a non-alphabetic character (so p.o. box -> P.O. Box).

Example (uses only char arrays, no globals):

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

int main() {
    const int MAX = 512;
    char line[MAX];
    std::cout << "Enter address: ";
    if (!std::cin.getline(line, MAX)) return 0;

    char token[MAX];
    int t = 0;
    for (int i = 0; i <= (int)strlen(line); ++i) {
        char c = (i == (int)strlen(line)) ? '#' : line[i];
        if (c != '#') token[t++] = c;
        else {
            token[t] = '\0';
            // trim
            int a = 0;
            while (token[a] && isspace((unsigned char)token[a])) ++a;
            int b = t - 1;
            while (b >= a && isspace((unsigned char)token[b])) --b;
            if (a <= b) {
                // lowercase everything first
                for (int k = a; k <= b; ++k)
                    token[k] = (char)tolower((unsigned char)token[k]);
                // uppercase letters at boundaries
                for (int k = a; k <= b; ++k) {
                    if (isalpha((unsigned char)token[k]) &&
                       (k == a || !isalpha((unsigned char)token[k-1])))
                        token[k] = (char)toupper((unsigned char)token[k]);
                }
                // print trimmed substring
                for (int k = a; k <= b; ++k) std::cout << token[k];
                std::cout << '\n';
            }
            t = 0;
        }
    }
    return 0;
}

Notes: cast to (unsigned char) before isalpha/isspace/toupper to avoid undefined behavior with negative char values. If you must preserve two-letter acronyms without dots (e.g. PO Box), add an extra pass to uppercase short all-alpha words; otherwise the algorithm matches the sample p.o. box case and keeps interior letters lowercase (so McDonald will become Mcdonald unless you add special rules).

Recommended Answers

All 2 Replies

If you're able to split each 'word' into separate strings, then you're already half way there

use the toupper function on the first character of that word, to generate the uppercase equivalent of that character (If an uppercase equivalent is available).

word[0] = toupper( word[0] );

This won't solve the problem of converting all letters in acronyms to uppercase; eg, a string of "p.o. box" will only be converted to "P.o. Box", since there is no whitespace character between the 'p' and the 'o'.

- Depending on the exact requirements of your assignment, you may need to do some additional string parsing for characters which follow punctuation.

I've gotten it to output in address format

Your computer must run quite a bit different than mine then.

Once you've entered your string

char word [80];
cin >> word;

or whatever size you think you need, then just cycle through all the characters changing # to carriage returns and anything after spaces and periods to upper case

int pntr;
for (pntr = 0; pntr < strlen (word); pntr++) 
     .... conditional code here
        word [pntr] ^= 0x20;

Invent whatever looping operation you like with DO, FOR or WHILE with either conditionals or SWITCH.

If your code would have at least output in the proper format I would have given you the solution, but it doesn't come anywhere near that.

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.