为什么这个用 c# 编写的反向脚本不起作用?
Why this reverse script written in c# isn't working?
我用 ubuntu 编写了这个 c# 代码。
///Uses swap method to reverse; need to traverse only half of the array.
public static stringReverseString2(string str)
{
char[] chars = str.ToCharArray();
for (int i =0, j = str.Length - 1; i < j; i++, j--)
{
char c = chars[i];
chars[i] = chars[j];
chars[j] = c;
}
return new string(chars);
}
但是当我编译它时我得到这个输出
c++1.c:2:1: error: expected unqualified-id before ‘public’
public static stringReverseString2(string str)
^
我该如何解决?
抱歉,我是 c# 新手
您的方法缺少 return 值
在 public static
之后,您需要指定您要 return 的类型:string
public static string stringReverseString2(string str)
或者您可以在名称中添加一个 space
public static string ReverseString2(string str)
将您的方法更改为如下所示:
public static string StringReverseString2(string str)
{
char[] chars = str.ToCharArray();
for (int i = 0, j = str.Length - 1; i < j; i++, j--)
{
char c = chars[i];
chars[i] = chars[j];
chars[j] = c;
}
return new string(chars);
}
您正在进行就地字符串反转,那么为什么要创建一个新字符串
改为使用字符串生成器,并向您的方法签名添加一个 return 类型
public static StringBuilder ReverseString2(StringBuilder str)
{
for (int i =0, j = str.Length - 1; i < j; i++, j--)
{
var c = str[i];
str[i] = str[j];
str[j] = c;
}
return str;
}
我认为您从 public static string ReverseString2(string str)
中错误地删除了 space。
你也可以这样做。
public static string ReverseString2(string str)
{
char[] chars = str.ToCharArray();
Array.Reverse(chars);
//for (int i = 0, j = str.Length - 1; i < j; i++, j--)
//{
// char c = chars[i];
// chars[i] = chars[j];
// chars[j] = c;
//}
return new string(chars);
}
我用 ubuntu 编写了这个 c# 代码。
///Uses swap method to reverse; need to traverse only half of the array.
public static stringReverseString2(string str)
{
char[] chars = str.ToCharArray();
for (int i =0, j = str.Length - 1; i < j; i++, j--)
{
char c = chars[i];
chars[i] = chars[j];
chars[j] = c;
}
return new string(chars);
}
但是当我编译它时我得到这个输出
c++1.c:2:1: error: expected unqualified-id before ‘public’
public static stringReverseString2(string str)
^
我该如何解决?
抱歉,我是 c# 新手
您的方法缺少 return 值
在 public static
之后,您需要指定您要 return 的类型:string
public static string stringReverseString2(string str)
或者您可以在名称中添加一个 space
public static string ReverseString2(string str)
将您的方法更改为如下所示:
public static string StringReverseString2(string str)
{
char[] chars = str.ToCharArray();
for (int i = 0, j = str.Length - 1; i < j; i++, j--)
{
char c = chars[i];
chars[i] = chars[j];
chars[j] = c;
}
return new string(chars);
}
您正在进行就地字符串反转,那么为什么要创建一个新字符串
改为使用字符串生成器,并向您的方法签名添加一个 return 类型
public static StringBuilder ReverseString2(StringBuilder str)
{
for (int i =0, j = str.Length - 1; i < j; i++, j--)
{
var c = str[i];
str[i] = str[j];
str[j] = c;
}
return str;
}
我认为您从 public static string ReverseString2(string str)
中错误地删除了 space。
你也可以这样做。
public static string ReverseString2(string str)
{
char[] chars = str.ToCharArray();
Array.Reverse(chars);
//for (int i = 0, j = str.Length - 1; i < j; i++, j--)
//{
// char c = chars[i];
// chars[i] = chars[j];
// chars[j] = c;
//}
return new string(chars);
}