在函数中传递char指针

时间:2015-02-26 23:07:26

标签: c++ pointers reference char swap

我试图在void函数中将一个char *与另一个char *交换。我不确定我做错了什么但是当我在主函数中打印char时输出没有加起来。 (我省略了我的" cout"行将产生输出以压缩我被困住的东西)

问题出在函数myOverwite()

功能:

#include <iostream>
using namespace std;

//returns size of string(# of characters)
int myLength(char *a){
  int i=0;
  while(a[i]!='\0'){
    i++;
  }
  return i;
 }
//reverse string
void myReverse(char *a){
  int m=myLength(a);
  if(m%2!=0){                   //char is not even
    m=((m-1)/2);
    for(int o=1;o<=m;o++){
      char temp=a[m-o];
      a[m-o]=a[m+o];
      a[m+o]=temp;
    }
    }else{
      m=m/2;
      int r=m;
      for(int l=(m-1);l>=0;l--){
      char temp=a[l];
      a[l]=a[r];
      a[r]=temp;
      r++;
    }
  }
}
//replaces one word with another
void myOverwrite(char *a,char* b){
  int aL=myLength(a);
  int bL=myLength(b);
  if(aL>=bL){
    char* temp=new char[aL];
    for(int i=0;i<aL;i++){
    temp[i]=a[i];
  }
 for(int i=0;i<aL;i++){
    if(i<bL){
      a[i]=b[i];
    }else{
      a[i]='\0';
    }
    b[i]=temp[i];
    delete[] temp;
  }
  }else{
    char* temp=new char[bL];
    for(int i=0;i<bL;i++){
      temp[i]=b[i];
    }
    for(int i=0;i<bL;i++){
      if(i<aL){
      b[i]=a[i];
    }else{
      b[i]='\0';
    }
    a[i]=temp[i];
  }
  delete[] temp;
  }
}
//joins first and second string and stores in place of first string
void myConcat(char* a, char* b){
}
//swap two strings
void mySwap(char *a,char* b){
}

MAIN:

int main(){
  char testI[]="Hello";
  char testII[]="Goodbye";
  char testIII[]="Monica";
  char testIV[]="Chase Bank";
  char testV[]="Pandemonium";

  //tests
  cout<<"FUNCTION\t\tIN\t\tOUTPUT\n"<<endl;
  //myLength()
  cout<<"myLength()\t\t";
  cout<<testI<<"\t\t"<<myLength(testI)<<endl;
  cout<<"\t\t\t"<<testII<<"\t\t"<<myLength(testII)<<endl;
  cout<<"\t\t\t"<<testIII<<"\t\t"<<myLength(testIII)<<endl;
  cout<<"\t\t\t"<<testIV<<"\t\t"<<myLength(testIV)<<endl;
  cout<<"\t\t\t"<<testV<<"\t\t"<<myLength(testV)<<endl;
  //myReverse()
  cout<<"\nmyReverse()\t\t";
  cout<<testI<<"\t\t";
  myReverse(testI);
  cout<<testI<<endl;
  myReverse(testI);
  cout<<"\t\t\t"<<testII<<"\t\t";
  myReverse(testII);
  cout<<testII<<endl;
  myReverse(testII);
  cout<<"\t\t\t"<<testIII<<"\t\t";
  myReverse(testIII);
  cout<<testIII<<endl;
  myReverse(testIII);
  cout<<"\t\t\t"<<testIV<<"\t\t";
  myReverse(testIV);
  cout<<testIV<<endl;
  myReverse(testIV);
  cout<<"\t\t\t"<<testV<<"\t\t";
  myReverse(testV);
  cout<<testV<<endl;
  myReverse(testV);
  cout<<"\nmyOverwrite()\t\t";

  cout<<"I= "<<testI<<"\t\tII= "<<testII<<endl;
  cout<<"\t\t\tI will switch with II"<<endl;
  myOverwrite(testI, testII);

  cout<<"\t\t\tI= "<<testI<<"\t\tII= "<<testII<<endl;

  cout<<"\n\t\t\tII= "<<testII<<"\t\tI= "<<testI<<endl;
  cout<<"\t\t\tII will now equal what I was"<<endl;
  myOverwrite(testII, testI);
  cout<<"\t\t\tII= "<<testII<<"\t\tI= "<<testI<<endl;

  cout<<"\n\t\t\tII= "<<testII<<"\t\tV= "<<testV<<endl;
  cout<<"\t\t\tII will now equal what V was"<<endl;
  myOverwrite(testII, testV);
  cout<<"\t\t\tII= "<<testII<<"\t\tV= "<<testV<<endl;

  cout<<"\n\t\t\tV= "<<testV<<"\t\tII= "<<testII<<endl;
  cout<<"\t\t\tV will now equal what II was"<<endl;
  myOverwrite(testV, testII);
  cout<<"\t\t\tV= "<<testV<<"\t\tII= "<<testII<<endl;

  return 0;
}

这是我的输出:

输出功能

myLength()          Hello           5
                    Goodbye         7
                    Monica          6
                    Chase Bank              10
                    Pandemonium             11

myReverse()         Hello           olleH
                    Goodbye         eybdooG
                    Monica          acinoM
                    Chase Bank              knaB esahC
                    Pandemonium             muinomednaP

myOverwrite()       I= Hello                II= Goodbye
                    I will switch with II

            i       temp    a       b       [0]
else(aL<bL)
  for(int i=0;i<bL;i++)
    temp[i]=b[i];
            0       [G]     [ ]     [G]     0
            1       [o]     [ ]     [o]     0
            2       [o]     [ ]     [o]     0
            3       [d]     [ ]     [d]     0
            4       [b]     [ ]     [b]     0
            5       [y]     [ ]     [y]     0
            6       [e]     [ ]     [e]     0
            i       temp    a       b       [0]
  for(int i=0;i<bL;i++)
   if t: b[i]=a[i];
      f: b[i]='
    a[i]=temp[i];
            0       [G]     [G]     [H]     0
            1       [o]     [o]     [e]     0
            2       [o]     [o]     [l]     0
            3       [d]     [d]     [l]     0
            4       [b]     [b]     [o]     0
            5       [y]     [y]     []      1
            6       [e]     [e]     []      1

                    I= Goodbye              II= Hello

                    II= Hello               I= Goodbye
                    II will now equal what I was
            i       temp    a       b       [0]
else(aL<bL)
   for(int i=0;i<bL;i++)
    temp[i]=b[i];
            0       [G]     [ ]     [G]     0
            1       [o]     [ ]     [o]     0
            2       [o]     [ ]     [o]     0
            3       [d]     [ ]     [d]     0
            4       [b]     [ ]     [b]     0
            5       [y]     [ ]     [y]     0
            6       [e]     [ ]     [e]     0
            i       temp    a       b       [0]
  for(int i=0;i<bL;i++)
   if t: b[i]=a[i];
      f: b[i]='
   a[i]=temp[i];
            0       [G]     [G]     [H]     0
            1       [o]     [o]     [e]     0
            2       [o]     [o]     [l]     0
            3       [d]     [d]     [l]     0
            4       [b]     [b]     [o]     0
            5       [y]     [y]     []      1
            6       [e]     [e]     []      1

                    II= Goodbye             I= Hello

                    II= Goodbye             V= Pandemonium
                    II will now equal what V was
            i       temp    a       b       [0]
    else(aL<bL)
      for(int i=0;i<bL;i++)
       temp[i]=b[i];
            0       [P]     [ ]     [P]     0
            1       [a]     [ ]     [a]     0
            2       [n]     [ ]     [n]     0
            3       [d]     [ ]     [d]     0
            4       [e]     [ ]     [e]     0
            5       [m]     [ ]     [m]     0
            6       [o]     [ ]     [o]     0
            7       [n]     [ ]     [n]     0
            8       [i]     [ ]     [i]     0
            9       [u]     [ ]     [u]     0
            10      [m]     [ ]     [m]     0
            i       temp    a       b       [0]
  for(int i=0;i<bL;i++)
   if t: b[i]=a[i];
     f: b[i]='
   a[i]=temp[i];
            0       [P]     [P]     [G]     0
            1       [a]     [a]     [o]     0
            2       [n]     [n]     [o]     0
            3       [d]     [d]     [d]     0
            4       [e]     [e]     [b]     0
            5       [m]     [m]     [y]     0
            6       [o]     [o]     [e]     0
            7       [n]     [n]     []      1
            8       [i]     [i]     []      1
            9       [u]     [u]     []      1
            10      [m]     [m]     []      1

                    II= Pandemoniumlo               V= Goodbye

                    V= Goodbye              II= Pandemoniumlo
                    V will now equal what II was
            i       temp    a       b       [0]
else(aL<bL)
   for(int i=0;i<bL;i++)
    temp[i]=b[i];
            0       [P]     [ ]     [P]     0
            1       [a]     [ ]     [a]     0
            2       [n]     [ ]     [n]     0
            3       [d]     [ ]     [d]     0
            4       [e]     [ ]     [e]     0
            5       [m]     [ ]     [m]     0
            6       [o]     [ ]     [o]     0
            7       [n]     [ ]     [n]     0
            8       [i]     [ ]     [i]     0
            9       [u]     [ ]     [u]     0
            10      [m]     [ ]     [m]     0
            11      [l]     [ ]     [l]     0
            12      [o]     [ ]     [o]     0
            i       temp    a       b       [0]
  for(int i=0;i<bL;i++)
   if t: b[i]=a[i];
      f: b[i]='
   a[i]=temp[i];
            0       [P]     [P]     [G]     0
            1       [a]     [a]     [o]     0
            2       [n]     [n]     [o]     0
            3       [d]     [d]     [d]     0
            4       [e]     [e]     [b]     0
            5       [m]     [m]     [y]     0
            6       [o]     [o]     [e]     0
            7       [n]     [n]     []      1
            8       [i]     [i]     []      1
            9       [u]     [u]     []      1
            10      [m]     [m]     []      1
            11      [l]     [l]     []      1
            12      [o]     [o]     []      1

                    V= Pandemoniumlohase Bank               II= Goodbye

我不确定发生了什么......

1 个答案:

答案 0 :(得分:2)

这里有很多输出,因此需要进行大量筛选。不一致的缩进使你和我们更难调试,但是一旦我自动缩进,它就跳出来了:你每次通过delete[] temp循环都会执行for

一个小小的评论是,NUL-填充较长的字符串一直到原始大小是不必要的;只有一个终止NUL是好的。

我的另一个不那么小的狡辩myOverwrite被错误地命名。交换是交换; &#39;覆盖&#39;不是真的(有什么?)。当你进入编程世界时,良好的命名非常重要。

相关问题