字符串反转 "you are beautiful" 转为 "beautiful are you"
2014-08-28 13:33
281 查看
//自己想的,从数学角度考虑每一个字符的位置,注意边界位置和空格的位置
void string_revert() { /* 8.27 2014 成功 要点: 数学 位置 精密 最后第一位要特别处理 算法 strDes[position+i-start]=strSrc[i]; */ char *strSrc="You are beautiful a girl Rocky afdafasf"; int len=strlen(strSrc); char *strDes=(char*)malloc(sizeof(char)*(len+1)); int position,start,end,single_len,i; start=end=0; position=len; cout<<"Source string: "<<strSrc<<endl; cout<<len<<endl; while(end<=len) { if(' '==strSrc[end] || '\0'==strSrc[end]) //end 到了空格就进入 { single_len=end-start; position=position-single_len; if('\0'==strSrc[end]) { cout<<position<<endl; for(i=start;i<end;i++) { strDes[position+i-start]=strSrc[i]; cout<<strSrc[i]; } break; } cout<<" space"<<end<<endl; cout<<" position"<<position<<endl; cout<<" word len"<<single_len<<endl; for(i=start;i<end;i++) { strDes[position+i-start]=strSrc[i]; cout<<strSrc[i]; } position=position-1; strDes[position]=' '; //排头第一个排除 start=end+1; } end++; } strDes[len]='\0'; cout<<"Destination String:"<<strDes<<endl; cout<<"lens of new string "<<strlen(strDes)<<endl; }
相关文章推荐
- 字符串反转 "you are beautiful" 转为 "beautiful are you" 方法二 更为简单
- 将字符串按照单词完全反转过来,如"abc"反转为"cba"
- 关于"Are you missing a call to unregisterReceiver()? "错误
- COCOS2D #error "Cannot recognize the target platform; are you targeting an unsupported platform?"
- Freemaker:you just want "true"/"false" result as you are generting computer-language output, use "?c
- Xcode中git的使用方法介绍与"Please tell me who you are"问题的解决方案
- HTTP Error 404.2 - Not Found "The page you are requesting cannot be served because of the ISAPI and
- 实现字符串中单词的逆转,即将单词出现的顺序进行逆转。如将"how are you "逆转为"you are how!"
- "there are stopped jobs" when you exit.
- 如何将"\u98de\u8f6c\u9080\u8bf7\u6392\u884c\u699c"这字符串转为中文
- Xcode中git的使用方法介绍与"Please tell me who you are"问题的解决方案
- "The file you are trying to open, '[filename]', is in a different format" Excel Error
- "三步反转"处理字符串
- C 反转字符串中的单词 "student a am i " --> "i am a student "
- Xcode中git的使用方法介绍与"Please tell me who you are"问题的解决方案
- android 将时间戳转为代表"距现在多久之前"的字符串
- C 反转字符串 "abcd" --> "dcba"
- android 将时间戳转为代表"距现在多久之前"的字符串
- Xcode中git的用法介绍与"Please tell me who you are"问题的解决方式
- “you are using old package ‘base' version 3.x"