我正在尝试为酒店创建一个程序,用户输入一个字符(S,D或L),并且该字符应该与下一行的代码相对应.我需要帮助转换用户输入(无论他们输入什么方式)转换为大写,然后我可以使用if语句来做我需要做的事情.到目前为止,我的代码如下:
public static void Main()
{
int numdays;
double total = 0.0;
char roomtype, Continue;
Console.WriteLine("Welcome to checkout. We hope you enjoyed your stay!");
do
{
Console.Write("Please enter the number of days you stayed: ");
numdays = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("S = Single, D = Double, L = Luxery");
Console.Write("Please enter the type of room you stayed in: ");
roomtype = Convert.ToChar(Console.ReadLine());
**^Right Her is Where I Want To Convert To Uppercase^**
total = RoomCharge(numdays,roomtype);
Console.WriteLine("Thank you for staying at …Run Code Online (Sandbox Code Playgroud) 我希望将句子的第一个单词转换为大写但不确定我将如何处理它.这是我到目前为止:
$o = "";
if((preg_match("/ibm/i", $m)) || (preg_match("/hpspecial/i", $m))) {
$o = strtoupper($m);
} else {
$o = ucwords(strtolower($m));
}
Run Code Online (Sandbox Code Playgroud)
我想uppercase从hpspecialID中使用,其中HP是句子的第一部分,但是这将大写整个句子.我如何才使HP的句子大写?
对于以下函数
void display()
{
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
if (board[i][j] < 84 && (i+j)%2 == 0)
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 0x70);
else if (board[i][j] < 84 && (i+j)%2 == 1)
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 0xc0);
else if (board[i][j] > 97 && (i+j)%2 == 0)
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 0x7c);
else if (board[i][j] > 97 && (i+j)%2 == 1)
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 0xc7);
cout …Run Code Online (Sandbox Code Playgroud) 所以这个程序的目标基本上是在终端(通过argv[])中获取一个 26 个字母的“键”,并使用它的索引作为替代指南。因此,您在终端中输入了 2 个输入,一个在 the 中argv[],一个只是普通get_string()输入。该argv[]输入将是这样的:./s YTNSHKVEFXRBAUQZCLWDMIPGJO这里s是文件名。然后get_string()输入如下所示:plaintext: HELLO。(输入是HELLO)。然后程序将循环遍历纯文本输入中的所有字母,并根据键的索引替换其字母索引argv[]。例如,H有一个按字母顺序排列的索引7(其中a= 0 和z= 25),因此我们查看键中的第 7 个索引YTNSHKV(E)FXRBAUQZCLWDMIPGJO,在本例中为E. 它对输入中的每个字母执行此操作,我们将得到输出ciphertext: EHBBQ。这是它在终端中的样子:
./s YTNSHKVEFXRBAUQZCLWDMIPGJO
plaintext: HELLO
ciphertext: EHBBQ
Run Code Online (Sandbox Code Playgroud)
但是我的输出是EHBB,因为当我使用toupper().
而且,大小写取决于明文输入,如果明文输入是hello, world,argv[]键是YTNSHKVEFXRBAUQZCLWDMIPGJO,则输出是jrssb, ybwsp,如果输入HellO, world具有相同的键,则输出是JrssB, ybwsp。
我基本上已经解决了这个问题,我的程序根据通过命令行输入的密钥将给出的明文替换为正确的密文。现在,假设明文输入是HELLO …
嗨伙计们在循环中实现Y/N或y/n时遇到一些麻烦.我的设计方式是用户可以在循环中使用Y和N的大小写字母作为答案.顺便说一句这里是我的代码,但似乎无法使其工作:
do
{
Console.WriteLine("\nSelect additional topping/s\n");
Console.WriteLine("1 - Extra meat: 200");
Console.WriteLine("2 - Extra cheese: 100");
Console.WriteLine("3 - Extra veggies: 80\n");
int selectedTopping = Convert.ToInt32(Console.ReadLine());
switch (selectedTopping)
{
case 1:
pizza = new MeatToppings(pizza);
break;
case 2:
pizza = new CheeseToppings(pizza);
break;
case 3:
pizza = new VeggieToppings(pizza);
break;
default:
break;
}
Console.WriteLine("\nAdd more toppings? Y/N");
}
while ((Console.ReadLine() == "Y") || (Console.ReadLine() == "y"));
Run Code Online (Sandbox Code Playgroud) 我在工作的前六个月编程,我仍然对标准和最佳实践有感觉。
在进行字符串或字符比较时,是使用 ToUpper 比较大写字符还是使用 ToLower 比较小写字符更常见?我可能更频繁地看到大写字母,但我一直在寻找更明确的答案,也许还有冗长的优化解释(较低的 ASCII 代码,无论如何)。
旁白:在我当前的任务中,我使用 string#replace 并且我的新字符串将是小写的以提高可读性,但这是否一定意味着我应该在我的源字符串和我正在寻找的子字符串上使用 ToLower?
我想知道是否还有像StringBuilder的toUpperCase这样的东西?下面是我的代码,我正在尝试用户输入一个短语并将其转换为首字母缩略词.有人通过建议StringBuilder帮助我,但我无法弄清楚是否有办法使首字母缩写大写.任何帮助是极大的赞赏.
public class ThreeLetterAcronym {
public static void main(String[] args) {
String threeWords;
int count = 0;
int MAX = 3;
char c;
//create stringbuilder
StringBuilder acronym = new StringBuilder();
Scanner scan = new Scanner(System.in);
//get user input for phrase
System.out.println("Enter your three words: ");
threeWords = scan.nextLine();
//create an array to split phrase into seperate words.
String[] threeWordsArray = threeWords.split(" ");
//loop through user input and grab first char of each word.
for(String word : threeWordsArray) {
if(count < …Run Code Online (Sandbox Code Playgroud) 我有一个练习,要求使用一个函数将字符串的所有字符转换为大写
System.Char.ToUpper
Run Code Online (Sandbox Code Playgroud)
首先,我将字符串更改为char数组,并将数组更改为字符列表
let x = s.ToCharArray()
List.ofArray x
Run Code Online (Sandbox Code Playgroud)
接下来,我想我会使用List.iter迭代我的列表并在每个字符上使用System.Char.ToUpper函数.
List.iter (fun z -> (System.Char.ToUpper(z)))
Run Code Online (Sandbox Code Playgroud)
但这不起作用.我得到一个错误'表达式应该有单位,但这里有char.' 我究竟做错了什么?它是逻辑或语法的缺陷吗?
对于我的优化,我想toupper在 Rcpp 中获得不错的成绩。我对 C++ 很陌生,至于知道我已经做到了:
#include <Rcpp.h>
using namespace Rcpp;
void C_toupper_String(const String& s) {
for (char *p =(char *)s.get_cstring();*p!=0;p++) *p = toupper(*p);
}
// [[Rcpp::export]]
StringVector C_toupper(StringVector const& vecteur) {
StringVector res=clone(vecteur);
for (int i(0); i < res.size(); ++i) {
C_toupper_String(res[i]);
}
return res;
}
/*** R
teststring <- "HeY I arNaud"
C_toupper(teststring)
toupper(teststring)
identical(C_toupper(teststring),toupper(teststring))
*/
Run Code Online (Sandbox Code Playgroud)
但是,它不能正常工作。
> C_toupper(teststring)
[1] "HEY I ARNAUD"
> toupper(teststring)
[1] "HEY I ARNAUD"
> identical(C_toupper(teststring),toupper(teststring))
[1] FALSE
Run Code Online (Sandbox Code Playgroud)
有什么问题?如果可能,我不想转换String为std::string …
我用 C 写了一个函数,用指针将所有小写字母转换成大写字母。它看起来像下面这样:
char *ft_strupcase(char *str)
{
char *str_begin;
str_begin = str;
while (*str != '\0')
{
if (*str >= 'a' && *str <= 'z')
{
*str = *str - 32;
}
str++;
}
return (str_begin);
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我使用以下主要功能进行测试,则会出现总线错误:
#include "ft_strupcase.c"
int main(void)
{
printf("The string \"I am your father\" is now %s\n", ft_strupcase("I am your father"));
return (0);
}
Run Code Online (Sandbox Code Playgroud)
错误消息如下所示:
/bin/sh: line 1: 79346 Bus error: 10
Run Code Online (Sandbox Code Playgroud)
但是如果我创建一个字符串变量来存储字符串,则没有总线错误。例如,如果我使用以下主要功能对其进行测试:
#include "ft_strupcase.c"
int main(void)
{
char test_string[50] = "I am your father"; …Run Code Online (Sandbox Code Playgroud)