用 c ,初级问题#include <stdio.h> void reverse(char * input) { char * end = input; while(*end != '\0') end ++; end --; // jump to end; char * start = input; // inverse. swap start and end. while(start < end){ char t = * start; *start = *end; *end = t; start ++; end --; } }void main() { char test[] = "I'm a student"; printf("%s\n", test); reverse(test); printf("%s\n", test); }
string str = "I am a student";string[] words = str.Split(' ');StringBuilder str2 = new StringBuilder();for(int i = words.Length - 1; i >= 0; i--) { str2.Append(words[i]); str2.Append(" "); }string result = str2.ToString();
string str = "I am a student"; string t="",k=""; for(int i=str.Length-1;i>=0;i--) { char c =str[i]; if(c==' ') { k+=t+c+' '; t=""; } else t=c+t; } k+=t; Console.WriteLine (k);
using System;public class t { public static void Main(string[] args) { string ss = "I am an student"; string[] ar = ss.Split(' '); for(int j = ar.Length,i = j-1 ; i >=0; i--) { Console.WriteLine(ar[i]); } } }
再转换成string
string ends="";
for (i=s.length-1;i<=0;i--)
{
ends+=s[i];
}
在C#中着样是错的
在C#中着样是错的
楼上看清楚原帖:string[] s="I am a student".Split(" ".toCharArray());
将字符内每个单词反转一次,不需要临时变量。
时间复杂度o(n),空间复杂度o(1)
{
/// <summary>
/// Class2 的摘要说明。
/// </summary>
public class Class2
{
public Class2()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
[STAThread]
static void Main(string[] args)
{
string str ="I am a student";
char[] arr = str.ToCharArray();
string lword="";
ReversalChar(arr);
for(int i = 0;i<arr.Length ;i++)
{
lword+=arr[i];
}
Console.WriteLine (lword); Console.Read ();
}
/// <summary>
/// // 返回字符串的长度
/// </summary>
/// <param name="strSource"></param>
/// <returns></returns>
static int Ustrlen(char[] strSource)
{
int iLength=0;
int i;
for(i=0;i<strSource .Length ;i++ )
{
if(strSource[i]!= '\0')
{
iLength++;
}
}
return iLength;
}
/// <summary>
/// 反转整个字符串
/// </summary>
/// <param name="strWord">待反转字符串</param>
/// <param name="iStart">旋转字符串开始位置</param>
/// <param name="iEnd">旋转字符串结束位置</param>
/// <returns>反转后字符串</returns>
static char[] _ReversalChar(char[] strWord,int iStart,int iEnd)
{
for(;iEnd > iStart;)
{
char ch;
ch = strWord[iStart];
strWord[iStart] = strWord[iEnd];
strWord[iEnd] = ch;
iStart++;
iEnd--;
}
return strWord;
} static char[] ReversalChar(char[] strWord)
{ int iLength = Ustrlen(strWord);
_ReversalChar(strWord,0,iLength-1);
int iStart=0,iEnd=0; for(int i = 0; i < iLength; i++)
{ if(strWord[i] == '\0')
{ iEnd = i+1;// if(iStart < iEnd)
// {
_ReversalChar(strWord,iStart,iEnd);
// } iStart = i-1;
} else if(strWord[i] == '!' || strWord[i] == ',' || strWord[i] == '.')
{
iStart = i+1;
}
}
return strWord;
}
}
}
我在第二次转是错了,帮我改改
void reverse(char * input)
{
char * end = input;
while(*end != '\0') end ++;
end --; // jump to end; char * start = input; // inverse. swap start and end.
while(start < end){
char t = * start;
*start = *end;
*end = t;
start ++;
end --;
}
}void main()
{
char test[] = "I'm a student";
printf("%s\n", test);
reverse(test);
printf("%s\n", test);
}
str2.Append(words[i]);
str2.Append(" ");
}string result = str2.ToString();
string t="",k="";
for(int i=str.Length-1;i>=0;i--)
{
char c =str[i];
if(c==' ')
{
k+=t+c+' ';
t="";
}
else
t=c+t;
}
k+=t;
Console.WriteLine (k);
StringBuilder Str=new StringBuilder(s.Length);
for(int i=s.Length-1,j=0,k=0; i>=0;i--)
{
if(Str.Length==0 |s[i]==' ')
{Str.Append(s[i]);j++;
if(s[i]==' ')k=j;
continue;}
if(Str[Str.Length-1]!=' ')
Str.Insert(k,s[i]);
else Str.Append(s[i]);
j++;
}
Console.WriteLine(Str.ToString());
{
public static void Main(string[] args)
{
string ss = "I am an student";
string[] ar = ss.Split(' ');
for(int j = ar.Length,i = j-1 ; i >=0; i--)
{
Console.WriteLine(ar[i]);
}
}
}
int temp;
for(int i=0;i<arr.Length/2;i++)
{
temp=arr[i];
arr[i]=arr[arr.Length-1-i];
arr[arr.Length-1-i]=temp;
}
//实现反转
http://community.csdn.net/Expert/topic/5049/5049811.xml?temp=.5682947
sring s[5],t=null;
int j=0;
for(i=0;i==11;i++)
{
u=u+or[i];
if(or[i]=' ')
{
s[j]=u;
u=null;
s[j+1]=' ';
j++;
}
s[5]=s[0];
s[0]=s[2];
s[1]=s[3]=' ';
}
Console.Writeline(s[0]+s[1]+s[2]+s[3]+s[4]);
sring s[5],t=null;
int j=0;
for(i=0;i==11;i++)
{
u=u+or[i];
if(or[i]=' ')
{
s[j]=u;
u=null;
s[j+1]=' ';
j++;
}
s[5]=s[0];
s[0]=s[4];
s[4]=s[5];
s[1]=s[3]=' ';
}
Console.Writeline(s[0]+s[1]+s[2]+s[3]+s[4]);
才发现少了两句交换语句
sring s[5],u=null;
int j=0;
for(i=0;i==11;i++)
{
u=u+or[i];
if(or[i]=' ')
{
s[j]=u;
u=null;
s[j+1]=' ';
j++;
}
s[5]=s[0];
s[0]=s[4];
s[4]=s[5];
s[1]=s[3]=' ';
}
Console.Writeline(s[0]+s[1]+s[2]+s[3]+s[4]);
反正帖子都后了 再改一次
{
static void Main()
{
string str = "I am a student";
string[] s = str.Split(new char[] { ' ' });
string str_temp = ""; for (int i = s.Length - 1; i > -1; i--)
{
str_temp = str_temp + s[i].ToString() + " ";
str = str_temp;
}
System.Console.WriteLine(str); }
}这样应该可以吧 呵呵
将整个字符反转一次
将字符内每个单词反转一次,不需要临时变量。
时间复杂度o(n),空间复杂度o(1) 他的思路行的通,结帖!