我是一名學生,剛接觸編程
我試圖使每個單詞的第一個字母大寫,其余字母小寫。
目前的輸出是即使我輸入了 2 個或更多單詞,也只列印了第一個單詞。
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
int i;
char str[100];
char rev;
int len = 0;
cout << "Enter a string: ";
cin >> str;
len = strlen(str);
for (i = 0; i < len; i )
{
if (i == 0)
{
str[i] = toupper(str[i]);
}
else
{
str[i] = tolower(str[i]);
}
cout << str[i];
}
}
uj5u.com熱心網友回復:
請注意,您的代碼仍然具有更多的“c”風味,然后是“c ”風味。仍然可以使用管理自己陣列的大小和使用索引,但最好留給標準庫的類。所以如果你想要一個字串,那么使用 std::string 而不是 char str[100]; 另請注意,字串具有長度方法,不需要將字串轉換為字符陣列。看看這個例子:
#include <string>
#include <set>
#include <iostream>
// do not teach yourself to type : using namespace std.
// just type std:: in front of every std type
// it will avoid problems in bigger projects later
// for anything you do make a function with a readable name.
// pass by value so we get a copy of the string to work on.
std::string capitalize(std::string string)
{
// create a static set of unique characters that will
// be used to recognize where new words begin.
// I added a few, maybe more are needed.
static std::set<char> delimiters{ ' ', ',', '.', '\n' };
// first character of a sentence should always be uppercase
bool make_next_char_uppercase = true;
// loop over all the characters in the input string.
// this is called a range based for loop and is
// an improvement over the indexed for loop
// used since "C", this style of loop can't go outside
// the bounds of the array.
// use a reference so we can modify the characters in place
for (char& c : string)
{
if (make_next_char_uppercase) c = toupper(c);
// the next line of code is C 20 syntax.
// but it means that if you encounter a word delimiter
// then the next character read must be changed to uppercase.
make_next_char_uppercase = delimiters.contains(c);
// for earlier versions of C use
//make_next_char_uppercase = (std::find(delimiters.begin(), delimiters.end(), c) != delimiters.end());
}
return string;
}
int main()
{
auto output = capitalize("the quick brown fox jumps over the lazy dog!\nalso text after a newline and a,should be uppercase!");
std::cout << output;
}
uj5u.com熱心網友回復:
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
int i;
char str[100];
char rev;
int len = 0;
cout << "Enter a string: ";
cin.getline(str, 100);
len = strlen(str);
for (i = 0; i < len; i )
{
if (i == 0)
{
str[i] = toupper(str[i]);
}
if (str[i] == ' ')
{
str[i 1] = toupper(str[i 1]);
}
cout << str[i];
}
}
uj5u.com熱心網友回復:
正如其他人指出的那樣,一旦std::cin遇到空間,它就會停止閱讀。
下面的代碼仍然一次讀取 1 個單詞。我喜歡它本質上如何為您進行字串拆分而無需您付出太多努力。
然后我使用iterator's 來遍歷std::string.
skip需要該函式來洗掉 中的任何空格,stream以便我可以檢查換行符 ( \n) 字符。如果檢測到換行符,則break退出回圈。
static std::istream& skip( std::istream& stream )
{
static const std::set<char> needles{ ' ', '\t', '\r', '\v' };
while ( needles.find( static_cast<char>( stream.peek( ) ) ) != needles.end( ) )
{
stream.ignore( );
}
return stream;
}
int main( )
{
const auto to_upper{ [ ]( auto begin, auto end ) {
std::transform( begin, end, begin, [ ]( unsigned char c ) {
return std::toupper( c );
} );
} };
const auto to_lower{ [ ]( auto begin, auto end ) {
std::transform( begin, end, begin, [ ]( unsigned char c ) {
return std::tolower( c );
} );
} };
std::cout << "Enter a strings\n";
for( std::string input{ }; std::cin >> input; )
{
to_upper( input.begin( ), input.begin( ) 1 );
to_lower( input.begin( ) 1, input.end( ) );
std::cout << input << ' ';
if ( std::cin >> skip && std::cin.peek( ) == '\n' )
{
std::cout << '\n';
break;
}
}
}
uj5u.com熱心網友回復:
std::cin當它看到一個空格時停止閱讀。如果您想閱讀到換行符,請使用getline(). 此外,如在 C 中使用std::string而不是 C 樣式字串。你不應該使用,using namespace std;因為它被認為是不好的做法,如這里所示。
我的“推薦”代碼:
#include <iostream>
#include <string>
#include <cctype>
int main()
{
std::string str;
char rev{};
std::cout << "Enter a string: ";
getline(std::cin, str);
int len {str.size()};
for (int i = 0; i < len; i )
{
if (i == 0)
{
str[i] = toupper(str[i]);
}
if (str[i] == ' ')
{
str[i 1] = toupper(str[i 1]);
}
std::cout << str[i];
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/329034.html
上一篇:為什么寫入此向量會引發例外?
