char *s和char s的区别与联系以及strlen和sizeof的用法
1个回答
展开全部
#include<iostream>
using namespace std;
void reverse(char s[])
{
char *p, *q, temp;
p = s;//p指向字符串s的第一个字符
q = s + strlen(s) -1;//q指向字符串s的最后一个字符。
while(p < q)
{
temp = *p;//交换两字符
*p = *q;
*q = temp;
p++;
q--;
}
}
int main()
{
char s[] = "ABCDE";
char *p = s;
char *q = s + strlen(s) - 1;
cout << "交换前:" << endl;
while(p <= q)
{
cout << *p <<" ";
p++;
}
cout << endl;
reverse(s);
char *p1 = s;
char *q1 = s + strlen(s) - 1;
cout << "交换后:" << endl;
while(p1 <= q1)
{
cout << *p1 <<" ";
p1++;
}
system("pause");
return 0;
}
using namespace std;
void reverse(char s[])
{
char *p, *q, temp;
p = s;//p指向字符串s的第一个字符
q = s + strlen(s) -1;//q指向字符串s的最后一个字符。
while(p < q)
{
temp = *p;//交换两字符
*p = *q;
*q = temp;
p++;
q--;
}
}
int main()
{
char s[] = "ABCDE";
char *p = s;
char *q = s + strlen(s) - 1;
cout << "交换前:" << endl;
while(p <= q)
{
cout << *p <<" ";
p++;
}
cout << endl;
reverse(s);
char *p1 = s;
char *q1 = s + strlen(s) - 1;
cout << "交换后:" << endl;
while(p1 <= q1)
{
cout << *p1 <<" ";
p1++;
}
system("pause");
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询