前言
在学习C语言的基础知识过程中,我们会做大量的练习来进行自我知识的掌握和能力的提升,随着代码量的积累和编程思想的逐步建立,在写代码的过程中思路会越来越清晰。写代码也会更加的得心应手。而我作为初次C语言的小白,和大家分享一个实现字符串的程序。不足之处,欢迎各位指正!
题目要求:
从终端获取一个具有“分隔符”的字符串,要求以单词为单位,进行翻转,并输出。
此处以“ ”为例。例如:"this is a book",输出-->"book a is this"
实现思路:
1、从终端获取字符串;
2、输出到终端获取到的字符串;
3、翻转全部字符;
4、将翻转后的字符串输出到终端;
5、实现单个单词的翻转。
注意事项:
1、一定要避免字符个数是奇数还是偶数的影响;
2、在进行单个单词翻转的时候,一定要定位准确此时获取的是哪个字符;
代码实现方式:
方式1:当分隔符较少时,可采用多次翻转单个字符。
方式2:采用标志位的方法来标记分隔符的个数,采取循环的方式翻转所有分隔符前的单词,最后再翻转最后一个单词。
方式3:直接采用循环的方式实现。
使用gets获取字符串会有友好地警告哟
方式1:示例:“this is a book”–>“koob a si siht”–>“book a is this”
#include <stdio.h>
int main()
{
char str[128] = {0};
printf("please input:\n");
gets(str);
printf("%s\n", str);
//第一步 先整体翻转
int i = 0;
int j = 0;
int k = 0;
char temp = 0;
// 先将循环变量i定位到最后一个字符,再实现全部翻转
while (str[i] != '\0')
{
i++;
}
i--;
// 采用三杯水交换的方式实现字符串的全部翻转
while (j < i)
{
temp = str[j];
str[j] = str[i];
str[i] = temp;
j++;
i--;
}
printf("%s\n", str);
// 翻转book
i = 0;
j = 0;
while (str[i] != ' ')
{
i++;
}
//使用变量k记录空格的位置 方便后面使用
k = i;
i--;
while (j < i)
{
temp = str[j];
str[j] = str[i];
str[i] = temp;
j++;
i--;
}
// 翻转a
k++;
i = k;
j = k;
while (str[i] != ' ')
{
i++;
}
//使用变量k记录空格的位置 方便后面使用
k = i;
i--;
while (j < i)
{
temp = str[j];
str[j] = str[i];
str[i] = temp;
j++;
i--;
}
// 翻转is
k++;
i = k;
j = k;
while (str[i] != ' ')
{
i++;
}
//使用变量k记录空格的位置 方便后面使用
k = i;
i--;
while (j < i)
{
temp = str[j];
str[j] = str[i];
str[i] = temp;
j++;
i--;
}
// 翻转this
k++;
i = k;
j = k;
while (str[i] != '\0')
{
i++;
}
i--;
while (j < i)
{
temp = str[j];
str[j] = str[i];
str[i] = temp;
j++;
i--;
}
printf("%s\n", str);
return 0;
}
代码实现结果:
linux@ubuntu:~/hqyj/base_c$
please input:
this is a book
this is a book
koob a si siht
book a is this
linux@ubuntu:~/hqyj/base_c$
方式2:示例:“this is a book”–>“koob a si siht”–>“book a is this”
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
char str[128] = {0};
puts("please input a string of charactors:");
gets(str);
printf("str = %s\n", str);
int i = 0;
int j = 0;
int k = 0;
char temp = 0;
// 标志位 统计有多少个空格
int flag = 0;
// 字符串全部翻转
while (str[i] != '\0')
{
if (str[i] == ' ')
{
flag++;
}
i++;
}
i = i--;
while (j < i)
{
temp = str[j];
str[j] = str[i - 1];
str[i - 1] = temp;
i--;
j++;
}
printf("str = %s\n", str);
// 实现遇到'\0'之前所有 空格 前面单词的翻转
i = 0;
j = 0;
while (flag)
{
while (str[i] != ' ')
{
i++;
} // i定位在 空格 的位置
k = i - 1; // k定位在 空格 前面的一个字符,用j、k翻转单词
i++; // i定位在 空格 后面的一个字符
while (j < k)
{
temp = str[j];
str[j] = str[k];
str[k] = temp;
k--;
j++;
} // 实现空格前的单词翻转
j = i; // j定位在 空格 后面的一个字符
flag--;
}
// 实现最后一个单词的翻转
while (str[i] != '\0')
{
i++;
}
k = i - 1;
while (j < k)
{
temp = str[j];
str[j] = str[k];
str[k] = temp;
k--;
j++;
}
printf("str = %s\n", str);
return 0;
}
代码实现结果:
linux@ubuntu:~/hqyj/base_c$ ./a.out
please input a string of charactors:
this is a book
str = this is a book
str = koob a si siht
str = book a is this
linux@ubuntu:~/hqyj/base_c$
方式3:示例:“this is a book”–>“koob a si siht”–>“book a is this”
#include <stdio.h>
int main(){
char str[32] = {0};
printf("请输入要翻转的字符串:\n");
gets(str);
printf("操作前:[%s]\n", str);
//第一步 先整体翻转
int i = 0;
int j = 0;
int k = 0;
char temp = 0;
while(str[i]){
i++;
}
i--;
while(j < i){
temp = str[j];
str[j] = str[i];
str[i] = temp;
j++;
i--;
}
printf("一步操作后:[%s]\n", str);
//再逐个单词翻转
i = 0;
j = 0;
while(1){
while(str[i] != ' ' && str[i] != '\0'){
i++;
}
k = i;//记录第一个的空格的位置 方便后面使用
i--;
//一个单词的翻转
while(j < i){
temp = str[j];
str[j] = str[i];
str[i] = temp;
j++;
i--;
}
if(str[k] == '\0'){
break;
}
//更新下标 准备找下一个单词
i = k+1;
j = k+1;
}
printf("操作后:[%s]\n", str);
return 0;
}
代码实现结果:
linux@ubuntu:~/hqyj/base_c$ ./a.out
请输入要翻转的字符串:
this is a book
操作前:[this is a book]
一步操作后:[koob a si siht]
操作后:[book a is this]
linux@ubuntu:~/hqyj/base_c$