1. 编写程序实现单词的倒置
"how are you" -> "you are how"
//1.整个字符串整体逆序
"uoy era woh"
//2.每个单词再逆序
#include <stdio.h>
#include <string.h>
void reverseStr(char *begin ,char *end)
{
while (begin < end)
{
char t = *begin;
*begin = *end;
*end = t;
++begin;
--end;
}
}
void reverseWord(char *s)
{
reverseStr(s,s+strlen(s)-1);
char *p = s;
while (*p != '\0')
{
char *q = p;
while (*p!=' '&&*p!='\0')
++p;
reverseStr(q,p-1);
if (*p == '0')
break;
++p;
}
}
int main(void)
{
//012345
//hello'\0'
//
char s[] = "how are you";
//reverseStr(s,s+strlen(s)-1);
reverseWord(s);
printf("s = %s\n",s);
return 0;
}
2. 编写一个程序实现功能:
将字符串”Computer Science”赋给一个字符数组,
然后从第一个字母开始间隔的输出该字符串,用指针完成。
Cmue cec
#include<stdio.h>
void fz(char*a)
{
while(*a!='\0')
{
printf("%c",*a);
a += 2;
if (*(a - 1) == '\0')
{
break;
}
}
}
int main(void)
{
char a[]="Computer Science";
fz(a);
return 0;
}