在函数中传递char指针
passing char pointer in function
我正试图在void函数中用另一个字符*交换一个字符。我不确定我做错了什么,但当我在主函数中打印char时,输出没有相加。(我省略了将产生输出的"cout"行,以浓缩我被卡住的内容)
问题出现在函数myOversite()中
函数:
#include <iostream>
using namespace std;
//returns size of string(# of characters)
int myLength(char *a){
int i=0;
while(a[i]!=' '){
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]=' ';
}
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]=' ';
}
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<<"FUNCTIONttINttOUTPUTn"<<endl;
//myLength()
cout<<"myLength()tt";
cout<<testI<<"tt"<<myLength(testI)<<endl;
cout<<"ttt"<<testII<<"tt"<<myLength(testII)<<endl;
cout<<"ttt"<<testIII<<"tt"<<myLength(testIII)<<endl;
cout<<"ttt"<<testIV<<"tt"<<myLength(testIV)<<endl;
cout<<"ttt"<<testV<<"tt"<<myLength(testV)<<endl;
//myReverse()
cout<<"nmyReverse()tt";
cout<<testI<<"tt";
myReverse(testI);
cout<<testI<<endl;
myReverse(testI);
cout<<"ttt"<<testII<<"tt";
myReverse(testII);
cout<<testII<<endl;
myReverse(testII);
cout<<"ttt"<<testIII<<"tt";
myReverse(testIII);
cout<<testIII<<endl;
myReverse(testIII);
cout<<"ttt"<<testIV<<"tt";
myReverse(testIV);
cout<<testIV<<endl;
myReverse(testIV);
cout<<"ttt"<<testV<<"tt";
myReverse(testV);
cout<<testV<<endl;
myReverse(testV);
cout<<"nmyOverwrite()tt";
cout<<"I= "<<testI<<"ttII= "<<testII<<endl;
cout<<"tttI will switch with II"<<endl;
myOverwrite(testI, testII);
cout<<"tttI= "<<testI<<"ttII= "<<testII<<endl;
cout<<"ntttII= "<<testII<<"ttI= "<<testI<<endl;
cout<<"tttII will now equal what I was"<<endl;
myOverwrite(testII, testI);
cout<<"tttII= "<<testII<<"ttI= "<<testI<<endl;
cout<<"ntttII= "<<testII<<"ttV= "<<testV<<endl;
cout<<"tttII will now equal what V was"<<endl;
myOverwrite(testII, testV);
cout<<"tttII= "<<testII<<"ttV= "<<testV<<endl;
cout<<"ntttV= "<<testV<<"ttII= "<<testII<<endl;
cout<<"tttV will now equal what II was"<<endl;
myOverwrite(testV, testII);
cout<<"tttV= "<<testV<<"ttII= "<<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
我不确定发生了什么…
这里有很多输出,所以需要筛选。不一致的缩进使您和我们更难调试,但一旦我自动缩进,它就会跳出来:每次通过顶部的for
循环都要执行delete[] temp
。
一个小的评论是,没有必要将较长的字符串一直NUL填充到其原始大小;只需一个终止NUL即可。
我的另一个不太小的疑问是myOverwrite
的名称不正确。掉期就是掉期"覆盖"实际上不是一件事(用什么?)。当你进入编程世界时,良好的命名非常重要。
相关文章:
- 构造函数 (C++) 中的 char 指针参数存在问题
- 将 char 指针传递给接受对 char 数组的引用的函数
- 分配值时,C++ char* 指针上的前递增与后递增
- char指针或char变量的默认值是多少
- 为什么为char指针分配内存不会截断char序列
- 愚蠢的问题:strcat和char指针
- 使用 ctypes 从函数返回字符串C++会给出大的 int,而不是 char 指针
- 打印char指针的Unicode和文本值
- 应对 std::字符串中的 std::<char>指针转换后的向量
- 一系列char指针
- 为什么可以将Char指针变量初始化为字符串,而INT指针变量不能初始化到整数数组
- 将char指针传递给cin和cin.get()
- 在C和C++中,将int值转换为char指针的差异
- std :: cout null char*指针中止该程序
- char指针违反访问权限
- const char指针在结构中声明
- 将字符串转换为 char 指针可以指向的 char 数组/值的最有效方法是什么
- 将Char指针数组传递到C 中的函数
- 获取错误C2440:char []* char*(指针)
- 删除Char指针在destructor中不起作用