hey ppl! i have got an assignment which asks me to do a personality evaluation on the basis of the users name and gender....i have to take user name input in upper or lower case....but have to output it in a proper manner with only the first letter of the first middle and last name being capital..
E.g. enter ur name: RagHav sInGh
name enterd is Raghav Singh...

i am new to c++ and really having a trble doing this...i wouldbe greatful if somebody could give me a solution to this...

Dani AI

Generated

A compact, robust approach is to (1) read the full line with getline, (2) trim and collapse extra whitespace, (3) validate characters (allow letters plus common name separators such as space, hyphen, apostrophe, period), and (4) lower-case everything then upper-case letters that start a word or follow a separator. That follows ’s direction to use tolower/toupper rather than fragile ASCII arithmetic, and addresses cases that ’s simple "capitalize after spaces" misses (hyphens, multiple spaces, initials). Note: ’s ASCII trick works on plain ASCII systems but is non-portable; avoid assuming contiguous ASCII ranges.

Example implementation (keeps things portable by casting to unsigned char before calling std::tolower/std::toupper/std::isalpha — this prevents undefined behavior on platforms where char is signed):

#include <string>
#include <cctype>

// remove leading/trailing whitespace, collapse runs of whitespace, and lower-case
std::string tidy(const std::string &in) {
    std::string out;
    std::size_t i = 0, n = in.size();
    while (i < n && std::isspace(static_cast<unsigned char>(in[i]))) ++i;
    bool saw_space = false;
    for (; i < n; ++i) {
        unsigned char ch = static_cast<unsigned char>(in[i]);
        if (std::isspace(ch)) { saw_space = true; continue; }
        if (saw_space && !out.empty()) out.push_back(' ');
        saw_space = false;
        out.push_back(static_cast<char>(std::tolower(ch)));
    }
    return out;
}

bool valid_name(const std::string &s) {
    for (std::size_t i = 0; i < s.size(); ++i) {
        unsigned char ch = static_cast<unsigned char>(s[i]);
        if (std::isalpha(ch) || ch==' ' || ch=='-' || ch=='\'' || ch=='.') continue;
        return false;
    }
    return true;
}

std::string title_case(const std::string &raw) {
    std::string s = tidy(raw);
    for (std::size_t i = 0; i < s.size(); ++i) {
        if ((i == 0 || s[i-1]==' ' || s[i-1]=='-' || s[i-1]=='\'' || s[i-1]=='.')
            && std::isalpha(static_cast<unsigned char>(s[i]))) {
            s[i] = static_cast<char>(std::toupper(static_cast<unsigned char>(s[i])));
        }
    }
    return s;
}

Troubleshooting notes and edge cases: cast characters to unsigned char before C locale functions to avoid UB; trim/collapse before validation so stray spaces don’t produce false errors; allow - and ' for double-barrel and O’/Mc names. For true Unicode-aware titlecasing (accents, non-Latin scripts) use a Unicode library such as ICU or Boost.Locale; the simple char/ctype approach only reliably handles locale-dependent single-byte characters. Special-name rules (Mc/Mac, O’, Irish/Scots prefixes, internal capitals) need bespoke exceptions if perfect styling is required.

Recommended Answers

All 12 Replies

>i wouldbe greatful if somebody could give me a solution to this...
We don't give freebies, but I'll point you in the right direction:

#include <cctype>
#include <iostream>
#include <string>

int main()
{
  std::string line;

  std::cout<<"Enter your a line of text: ";

  if ( getline ( std::cin, line ) ) {
    // Make the line all upper case
    for ( std::string::size_type i = 0; i < line.size(); i++ )
      line[i] = std::toupper ( line[i] );

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

There's also a corresponding tolower function that does the reverse.

Switching from capital letter to small letter and vise versa is easy:
The letter is capital if it's between 'A' and 'Z' (inclusive)(ASCII symbols' values are in order) and the letter is small letter if it's between 'a' and 'z' (inclusive).
So, to change a small letter to a capital, you just subtract ('a'-'A' -- the ASCII difference between small and capital. Note that the value of lower-case letters is greater), and to change from capital to lower-case letter you add 'a'-'A'.

Example snippet:

string Input;
cout<<"Input your name\n";
getline(cin, Input);

if(Input[0]>='a' && Input[0]<='z') Input[0]-='a'-'A';
 for(int x=1; x<Input.length(); x++)
 {
 if(Input[x]>='a' && Input[x]<='z' && Input[x-1]==' ') Input[x]-='a'-'A';
 else if(Input[x]>='A' && Input[x]<='Z') Input[x]+='a'-'A';
 }

>So, to change a small letter to a capital, you just subtract ('a'-'A' --
>the ASCII difference between small and capital. Note that the value
>of lower-case letters is greater), and to change from capital to
>lower-case letter you add 'a'-'A'.
Or you can just use tolower and toupper, and not have to hope that the character set matches the rules for ASCII letters. Note that 'A' to 'Z' and 'a' to 'z' being consecutive is not a safe assumption.

Or you can just use tolower and toupper, and not have to hope that the character set matches the rules for ASCII letters. Note that 'A' to 'Z' and 'a' to 'z' being consecutive is not a safe assumption.

Correct me if I'm wrong, but ASCII 1 to 128 is standard and is the same for all computers, unlike 129-255.
Using the following code, you can see it's in the standard range:

for(unsigned char c=10;c<255;c++)cout<<(int)c<<". "<<c<<"\n";

Read carefully..

string name
k=0
for i to namelength
begin
   if name[i]='space' or i=0 or i=namelength then
   begin
      j=(i+k)/2
      name[j]=toupper(name[j])
      k=i
   end
end
if name[i]='space' or i=0 or i=namelength then

Why should the last letter be capital?

Why should the last letter be capital?

Read again..U will know..

>Correct me if I'm wrong, but ASCII 1 to 128 is standard and is the same for all computers
You're wrong, and I already corrected you. Just because your system uses ASCII doesn't mean everyone else's system uses ASCII. C++ is defined to work on all of them.

>Read carefully..
Why should I read carefully when you obviously can't? I can only assume you read the problem backward. Props for trying to find a pattern in random input though. :icon_rolleyes:


>Read carefully..
Why should I read carefully when you obviously can't? I can only assume you read the problem backward. Props for trying to find a pattern in random input though.

???..Yeah..U right..I dont finish to read its problems..
???...???...

string name
name[0]=toupper(name[0])
for i=0 to namelen
begin
   if name[i]='space' then
   begin
      i=i+1
      name[i]=toupper(name[i])
   end
   i=i+1
end

thanks so much guys..

thx so much guy..i have almost made it.... i have to give an error msg id name contains an integer or a symbol....i have tries the while loop but it doesnt works....can ne1 chk

int main(int argc, char *argv[])
{
    string n;
    string s;
 
 {   

  cout<<"Please enter your name :"<<flush;
  getline(cin, n);
  
  for(int i=1; i<n.length(); i++)
  while(n[i]<='A'|| n[i]>='z');
  
  cout<<"Error code 01: Name contains a character that is neither an alphabet nor a space"<<endl;
  cout<<"Please enter your name :"<<flush;
  getline(cin, n);
   if(n[0]>='a' && n[0]<='z') n[0]-='a'-'A';


   for(int x=1; x<n.length(); x++)
   {
            if(n[x]>='a' && n[x]<='z' && n[x-1]==' ') n[x]-='a'-'A';
             else if(n[x]>='A' && n[x]<='Z') n[x]+='a'-'A';
  }

 cout<<n<<endl;

}

thx so much guy..i have almost made it.... i have to give an error msg id name contains an integer or a symbol....i have tries the while loop but it doesnt works....can ne1 chk

// you forgot add the following:
#include<iostream>
#include<string>
using namespace std;

int main(int argc, char *argv[])
{
    string n;
    string s;
 
 {   // <--- this bracket is unnecessary

  cout<<"Please enter your name :"<<flush;
  getline(cin, n);
  
  for(int i=1; i<n.length(); i++)

/*
  while(n[i]<='A'|| n[i]>='z');
  
  cout<<"Error code 01: Name contains a character that is neither an alphabet nor a space"<<endl;
  cout<<"Please enter your name :"<<flush;
  getline(cin, n);
*/

// correction:
if(n[i]>'z' || n[i]<'A' && n[i]!=' ')
{
  cout<<"Error code 01: Name contains a character that is neither an alphabet nor a space"<<endl;
  cout<<"Please enter your name :"<<flush;
  getline(cin, n);
}

   if(n[0]>='a' && n[0]<='z') n[0]-='a'-'A';


   for(int x=1; x<n.length(); x++)
   {
            if(n[x]>='a' && n[x]<='z' && n[x-1]==' ') n[x]-='a'-'A';
             else if(n[x]>='A' && n[x]<='Z' /* it might be goot to add " && n[x-1]!=' ' " */) n[x]+='a'-'A';
  }

 cout<<n<<endl;

}

The green (// and /* */) parts are the corrections.

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.