-
name program
Let's say i typed in CHRIS
then i want the program to print out Chris
make lower case everyting except first letter of the name
ty
Code:
#include <iostream>
#include <string>
using namespace std;
char upper(char upper);
char lower(char lower);
int main(void)
{
string first, middle, last, whole;\
long number;
cout <<"\nWhat is your name?\n ";
cin >> first >> middle;
if (cin.peek() != '\n')
{
cin >>last;
}
else
{
last = middle;
middle = "";
}
cout << upper(last[0]);
last = last.substr(1);
number = 0;
while (number < last.length())
{
last += lower(last[number]);
number++;
}
//whole = last + ',' + first + ' ' + middle[0] + '.';
cout << whole;
return 0;
}
char upper(char upper)
{
return toupper(upper);
}
char lower(char lower)
{
return tolower(lower);
}
-
And what was the question?
-
how do i change big case to lower case?
-
this code is suppose to find all the charcters except first letter of the name
and change to lower case... but it doesnt work...T_T
Code:
while (number < last.length())
{
last += lower(last[number]);
number++;
}
-
It seems to me you're modifying (and making longer) the same string that you're reading from.
Read from one string, and write to another.
-
This should do the trick:
Code:
int NameFormat(char* pszString, DWORD dwString)
{
if(dwString < 1 || !pszString)
return 1;
if((pszString[0] >= 'a') && (pszString[0] <= 'z'))
pszString[0] += ('A' - 'a');
for(DWORD dwLoop = 1; dwLoop < dwString; dwLoop++)
if((pszString[dwLoop] >= 'A') && (pszString[dwLoop] <= 'Z'))
pszString[dwLoop] -= ('A' - 'a');
return 0;
}
-
This should also do the trick:
Code:
#include <iostream>
#include <algorithm>
#include <cctype>
using namespace std;
//We have to wrap tolower so that transform can determine its type
char tolower_wrap(char c)
{
return tolower(c);
}
int main()
{
string foo("CHRIS");
//Use transform to make each char after the first one lowercase
transform(foo.begin()+1, foo.end(), foo.begin()+1, tolower_wrap);
//Did it work?
cout <<foo <<endl;
}
-
>> This should also do the trick:
Phahch, mine is way more portable.
-
> Phahch, mine is way more portable.
Yours isn't portable at all with all that 'A'-'a' stuff. Would it work with EBCDIC?
At least using tolower() is an improvement.
-
Code:
char my_tolower(char c)
{
char letters[52] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
for(int i = 0; i < 26; i++)
{
if(c == letters[i])
{
return letters[i + 26];
}
}
return c;
}
There you go. Completely portable.
-
Just to be contrary:
bläck
BLÄCK