文章目录倒置字符串
思路
1 .先整体倒置 I like beijing 倒置后 gnijieb ekil i
2,逐个单词倒置 beijing like i
- 技能点
- 代码实现
- 总结
技能点
1:指针的使用 2:gets和scanf的区别
1.指针的使用:
char a[100] 表示定义一个数组,开辟100个连续的内存空间 。其中数组名为a
a 表示数组首元素的地址 *start=&a, start 表示指向数组a的指针 *start表示数组的第一个元素,这里是 I
2:gets和scanf的区别
gets
Get a line from the stdin stream
头文件为
gets | scanf |
---|---|
Get a line from the stdin stream | Read formatted data from the standard input stream |
头文件 | |
char *gets( char *buffer ); | int scanf( const char *format [,argument]… ) |
Return Value Each of these functions returns its argument if successful. A NULL pointer indicates an error or end-of-file condition. Use ferror or feof to determine which one has occurred. | Return Value Both scanf and wscanf return the number of fields successfully converted and assigned; the return value does not include fields that were read but not assigned. A return value of 0 indicates that no fields were assigned. The return value is EOF for an error or if the end-of-file character or the end-of-string character is encountered in the first attempt to read a character. |
# define _CRT_SECURE_NO_WARNINGS 1
// 把 I LOVE YOU 改写成 YOU LOVE I
#include
//逆序函数,只要传入首位字符的位置就可以实现逆序
void reverse(char* left ,char *right )
{
//一次翻转
while (left < right)
{
char tem = *left;
*left = *right;
*right = tem;
left++;
right--;
}
}
void reverse_string(char* a)
{
reverse(a, a+strlen(a)-1);//2.先整体逆序
//2.每个单词逆序
while (*a)
{
//单词逆序
char* start = a;
char* end = a;
while (*end != ' ' && *end != ')'++
{
end;//指针++,指向下一个位置}
//结束上述循环时end指向空格或者reverse
(
,-start1 end ) ;//传参时指针//下一个单词的逆序if
(
* !=')'end //如果不是最后一个单词,则 end+1作为新一个单词的开头 =+1
{
a ; end //空格后面的一个 }else//是最后一个单词
=
;}
{
a} end}
int
main
(
) char[100
{
] a=0} ; { gets ()
;//输入一个字符串areverse_string()
;printfa("%s"
,);return a0;
}
总结
以上就是今天要讲的内容,本文用一个例题简单介绍了指针的使用,欢迎指正交流。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)