c语言输出char字符倒序输出,字符串中的每个单词倒序输出问题

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

#include

void sub_reverse(char *);

int main()

{

char str[256] = "The only way to learn a new programming language is by writing programs in it";

char string[256] = "Oh,My god!What are you doing here? \"Come boy!\"";

printf("%s\n",str);

sub_reverse(str);

printf("%s\n",str);

printf("%s\n",string);

sub_reverse(string);

printf("%s\n",string);

return 0;

}

void sub_reverse(char *s)

{

char *pfstchr;

char *plstchr;

char *head;

char *end;

char tmp;

pfstchr = s;

while(*pfstchr!='\0')

{

if(*pfstchr==0x20||*pfstchr==','||*pfstchr=='.'||*pfstchr=='?'||*pfstchr=='!'||*pfstchr==';')

{

*pfstchr++;

}

else

{

plstchr = pfstchr + 1;

while(*plstchr!=0x20&&*plstchr!=','&&*plstchr!='.'&&*plstchr!='?'&&*plstchr!='!'&&*plstchr!=';'&&*plstchr!='\0')

{

plstchr++;

}

head = pfstchr;

end = plstchr - 1;

while(head

{

tmp = *head;

*head = *end;

*end = tmp;

head++;

end--;

}

pfstchr = plstchr;

}

}

}

The only way to learn a new programming language is by writing programs in it

ehT ylno yaw ot nrael a wen gnimmargorp egaugnal si yb gnitirw smargorp ni ti

Oh,My god!What are you doing here? "Come boy!"

hO,yM dog!tahW era uoy gniod ereh? emoC" yob!"

要实现C语言字符数组字符串倒序输出,可以通过以下步骤来完成: 1. 首先,声明一个字符数组来存储字符串。例如,可以使用`char str[] = "Hello World";`来初始化一个字符串。 2. 然后,使用一个循环来计算出字符串的长度。可以使用`strlen()`函数来获取字符串的长度,需要包含头文件`<string.h>`。 3. 接下来,使用一个新的字符数组来存储序后的字符串。可以声明一个与原字符串长度相同的字符数组,用于存储序后的字符串。 4. 使用两个指针,一个指向原字符串的开头位置,另一个指向序后字符串的末尾位置。 5. 在一个循环,从原字符串的末尾开始,逐个字符复制到序后的字符串,直到原字符串的开头位置。同时,要记得将序后字符串的指针向前移动。 6. 最后,在循环结束后,序后的字符串就存储在新的字符数组。可以使用`printf()`函数输出序后的字符串。 下面是一个示例代码实现: ```c #include <stdio.h> #include <string.h> int main() { char str[] = "Hello World"; int length = strlen(str); // 计算字符串的长度 char reverseStr[length + 1]; // 创建一个新的字符数组来存储序后的字符串,+1 是为了存储结束符'\0' char *pSource = str + length - 1; // 指向原字符串的末尾位置 char *pDestination = reverseStr; // 指向序后字符串的开头位置 while (length > 0) { *pDestination = *pSource; // 从原字符串复制字符序后的字符串 pSource--; // 移动原字符串指针 pDestination++; // 移动序后字符串指针 length--; // 字符串长度减1 } *pDestination = '\0'; // 添加字符串结束符 printf("倒序输出字符串:"); printf("%s", reverseStr); return 0; } ``` 以上就是使用C语言实现字符串倒序输出的详细步骤和示例代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值