题解 | #字符串变形#

字符串变形

http://www.nowcoder.com/practice/c3120c1c1bc44ad986259c0cf0f0b80e

  1. 先把整个字符串翻转
  2. 在按照空格分段,改变大小写,部分反转
//翻转函数
void reverse(char* s,int head,int tail){
    char temp;
    while(head<tail){
        temp=s[head];
        s[head]=s[tail];
        s[tail]=temp;
        head++;
        tail--;
    }
}
//主函数
char* trans(char* s, int n ) {
    //翻转整个字符串
    reverse(s,0,n-1);
    int i=0,j=0;
    while(i<n){
        j=i;
        //按照空格分段进行大小写切换,部分反转
        while(j<n && s[j] != ' '){
        	//切换大小写
            if(s[j]>='A' && s[j]<='Z'){
                s[j]+=32;
            }else if(s[j]>='a' && s[j]<='z'){
                s[j]-=32;
            }
            j++;
        }
        //把这一段翻转
        reverse(s,i,j-1);
        i=j+1;//i变为空格后一个
    }
    return s;
}
全部评论

相关推荐

12-11 14:24
门头沟学院 Java
牛客35720396...:不要用boss,全是骗
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
22
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务