一道处理字符串的面试题

    技术2022-05-19  27

    编写如下形式的字符串处理函数,要求将S1指向的字符串倒向复制给S2,如*S1 = "hello",则使 *S2 = "olleh"。且不能使用除S1,和S2以外的其余任何变量。还不能使用任何strlen等库函数。void ReverseStr( const char* s1 ,char* s2);

     

    #include <iostream> using namespace std; void ReverseStr(const char *src, char *dst) { if(*src != '/0') //若src不为空串 { ReverseStr(src + 1, dst); while(*dst != '/0') //找到dst的末端位置 dst++; *dst = *src; //将×src放到dst的末端位置 *(dst + 1) = '/0'; //设置最后一个字符 } else //若src为空串 *dst = '/0'; } int main() { const char *src = "hello"; char dst[10] = {0}; ReverseStr(src, dst); cout<<dst<<endl; return 0; }  

     

     

    到100以内连续相加和等于100的数输出

     

    #include <iostream> using namespace std; void print(int begin, int end) { for(int i=begin; i<=end; i++) { cout<<i<<" "; } cout<<endl; } /* 函数功能:1到100以内连续相加和等于100的数输出 利用等差数列的公式:sum=(a+b)*n/2; */ void SequenceSum() { for(int i=1; i<=50; i++) { for(int j=i+1; j<50; j++) { if((i+j)*(j-i+1) == 200) print(i, j); } } } int main() { SequenceSum(); }  

    http://topic.csdn.net/u/20110304/00/149b3fac-8c99-4814-acc9-309f2d81333e.html?seed=2040050762&r=71994762#r_71994762

     


    最新回复(0)