反轉單詞 C 實現

2022-08-12 15:21:18 字數 1305 閱讀 3394

public

static

string

reversewords(

string

array)if(

string

.isnullorempty(array))

throw

newargumentexception(""

);int

arrlen = array.length;

char

strnew =

newchar

[arrlen + 1];

//全部反轉

for(

intindex = 0; index < arrlen; index++)

strnew[index] = array[arrlen - 1 - index];

strnew[arrlen] =

' ';

//為了復原最後乙個單詞,新增乙個輔助空格

intistart = 0, iend = 0;

char

tmpchar;

//復原單詞

for(

intindex = 0; index < arrlen + 1; index++)

//index指向空格

if(strnew[index] ==

' ')

iend = index - 1;

while

(istart < iend)

tmpchar = strnew[istart];

strnew[istart] = strnew[iend];

strnew[iend] = tmpchar;

istart++;

iend--;

istart = index + 1;

//跳過空格

else

if(strnew[index] ==

'!'| strnew[index] ==

','| strnew[index] ==

'.'| strnew[index] ==

';')

istart = index + 1;

//跳過標點

//合成字串

array =

string

.empty;

for(

intindex = 0; index < strnew.length - 1; index++)

//去掉輔助的空格

array += strnew[index];

return

array;

A 單詞反轉

a.單詞反轉 time limit 1000 ms memory limit 65536 kb total submissions 669 accepted 326 description 給你一些英文句子,請將這些句子中的每個英語單詞反轉,然後再將其輸出。這裡所說的英語單詞僅由大 小寫英文本母組成...

反轉單詞順序

given an input string,reverse the string word by word.for example,given s the sky is blue return blue is sky the click to show clarification.clarifica...

單詞的反轉

刷題,求單詞的翻轉。利用到了程式設計珠璣上的翻手法則。主要思路是先將字串整體翻轉,再在單詞內部翻轉 先單詞翻轉,在整體翻轉貌似也行 include include include char string reverse char first,char last return ptr char str...