PASCAL:将用户输入的字符串转换为第一个字母大写,其余字母小写。我怎样才能做到这一点?
PASCAL: converting user input string to first letter uppercase, the rest lowercase. How can I do this?
正如标题所说。
我正在编写一个程序,要求用户输入姓氏,然后是名字,最后是地址。
完成后,它会打印出 table 结果,然后按姓氏、名字和地址的字母顺序排列。
这一切都完成了。我只需要让 table 总是以第一个字母大写,其余字母小写打印出来,即:
输入:约翰·史密斯
输出:约翰·史密斯
我如何在 Pascal 中执行此操作?
这是我到目前为止为这部分编写的代码:
writeln();
write(UpCaseFirstChar(arrSurnames[count]):15);
write(UpCaseFirstChar(arrFirstNames[count]):15);
write(UpCaseFirstChar(arrAddress[count]):30);
writeln();
我有一个将第一个字母大写的功能,我怎样才能把它改成小写其余的字母?
编辑:这是大写函数:
function UpCaseFirstChar(const S: string): string;
begin
Result := S;
if Length(Result) > 0 then
Result[1] := UpCase(Result[1]);
end;
编辑 2:我想我明白了。如果有人感兴趣,这里是 UpCase/LowerCase 函数的新代码:
function UpCaseFirstChar(const S: string): string;
var
i: integer;
begin
Result := S;
if Length(Result) > 0 then
Result[1] := UpCase(Result[1]);
for i := 2 to Length(Result) do
begin
Result[i] := LowerCase(Result[i]);
end;
end;
您的更新比需要的更详细。如果您仔细阅读文档,函数 LowerCase
适用于 字符串 。所以你可以写:
function UpCaseFirstChar(const S: string): string;
begin
if Length(S) = 0 then
Result := S
else begin
Result := LowerCase(S);
Result[1] := UpCase(Result[1]);
end;
end;
正如标题所说。
我正在编写一个程序,要求用户输入姓氏,然后是名字,最后是地址。
完成后,它会打印出 table 结果,然后按姓氏、名字和地址的字母顺序排列。
这一切都完成了。我只需要让 table 总是以第一个字母大写,其余字母小写打印出来,即:
输入:约翰·史密斯 输出:约翰·史密斯
我如何在 Pascal 中执行此操作?
这是我到目前为止为这部分编写的代码:
writeln();
write(UpCaseFirstChar(arrSurnames[count]):15);
write(UpCaseFirstChar(arrFirstNames[count]):15);
write(UpCaseFirstChar(arrAddress[count]):30);
writeln();
我有一个将第一个字母大写的功能,我怎样才能把它改成小写其余的字母?
编辑:这是大写函数:
function UpCaseFirstChar(const S: string): string;
begin
Result := S;
if Length(Result) > 0 then
Result[1] := UpCase(Result[1]);
end;
编辑 2:我想我明白了。如果有人感兴趣,这里是 UpCase/LowerCase 函数的新代码:
function UpCaseFirstChar(const S: string): string;
var
i: integer;
begin
Result := S;
if Length(Result) > 0 then
Result[1] := UpCase(Result[1]);
for i := 2 to Length(Result) do
begin
Result[i] := LowerCase(Result[i]);
end;
end;
您的更新比需要的更详细。如果您仔细阅读文档,函数 LowerCase
适用于 字符串 。所以你可以写:
function UpCaseFirstChar(const S: string): string;
begin
if Length(S) = 0 then
Result := S
else begin
Result := LowerCase(S);
Result[1] := UpCase(Result[1]);
end;
end;