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...

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.