C++ 轉大寫... 20pt, 急!!!

2008-09-21 4:29 pm
Hello. I am new to C++ and I have been spend 4+ hours in the following problem. And I am seeking for help in here. It is due by tonight so please help...

How do I modify the following structure to a program which can...
Part A) Capitalize all the input characters?
Part B) Capitalize only the first letter of each word?

#include <stdio.h>
#include <iostream>
using namespace std;

int main (int argc, char *argv[], char **env)
{
char c, lastc = ' ' ;
c = cin.get() ;
do {
/* conditional expression and converter logic goes here */
cout.put(c) ;
//lastc = c ; // THIS IS IMPORTANT FOR PART B.
c = cin.get() ;
} while ( !cin.eof()) ;
}

Here is the instruction:
Use cin.get() to read (input) from the keyboard character by character
Use the int toupper(int) function to change the alpha characters to uppercase.
Use if (isalpha(c)) to decide if a character is alphabetic.
Lastly, use the cout.put(char) function to output the char.

回答 (1)

2008-09-22 8:16 am
✔ 最佳答案
Part A) Capitalize all the input characters?

#include <iostream.h>
#include <stdlib.h>
#include <ctype.h> // *******ADD THIS LINE
int main()
{
char c, lastc = ' ' ;
c = cin.get() ;
do {
/* conditional expression and converter logic goes here */

c=toupper(c); // *******ADD THIS LINE
cout.put(c) ;
//lastc = c ; // THIS IS IMPORTANT FOR PART B.
c = cin.get() ;
} while ( !cin.eof()) ; // use ENTER to termine line, ctrl-Z to terminate input
system("PAUSE");
return 0;
}


Part B) Capitalize only the first letter of each word?

#include <iostream.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
char c, lastc = ' ' ; // VERY IMPORTANT
c = cin.get() ;
do {
/* conditional expression and converter logic goes here */

if(lastc==' ')c=toupper(c); // *** CONVERT ONLY IF PREVIOUS CHAR. IS ' '
cout.put(c) ;
lastc = c ; // THIS IS IMPORTANT FOR PART B.
c = cin.get() ;
} while ( !cin.eof()) ;
system("PAUSE");
return 0;
}


PM me if you have questions.


收錄日期: 2021-04-13 16:05:25
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20080921000051KK00419

檢視 Wayback Machine 備份