My AC C codes. thanks for more functional algorithm


  • -4
    W

    void reverse(char *s,int begin,int end){
    if(*s=='\0')
    return;
    char temp=0;
    while(begin<end){
    temp=s[begin];
    s[begin]=s[end];
    s[end]=temp;
    begin++;end--;
    }
    }
    void trim(char *s)
    {
    int begin=0,end=0,len=0;
    while(s[begin]==' ') begin++;//to first letter
    while(s[end]) end++;//to the end
    while(s[--end]==' ');//to the last letter
    for(len=0;begin<=end;begin++)//move the single space and the letter
    {
    if(s[begin]==' '&&s[begin+1]==' ')
    continue;
    else s[len++]=s[begin];
    };
    s[len]='\0';//the sign of end
    }
    void reverseWords(char *s) {
    int i=0,j=0;
    trim(s);
    if(*s=='\0')
    return;
    while(s[j+1]){
    j++;
    };
    reverse(s,i,j);
    i=0;j=0;
    while(s[j]){
    if(s[j+1]==' '||s[j+1]=='\0'){
    reverse(s,i,j);
    i=j+1;
    if(s[i]==' ')
    i++;
    else break;
    };
    j++;
    }
    }


  • 0
    R

    Can't you use proper markdown syntax to organize your code?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.