当前位置:网站首页>Two methods of implementing inverted strings in C language

Two methods of implementing inverted strings in C language

2022-08-09 22:07:00 Code Chen Shuai

目录

前言:

一、题目

事例:

二、思路讲解 

三、代码实现

1.Create an array to store the input string

2、Invert each word 

How the statement ends the loop:

The start and end positions of each word :

内部怎么实现:

 How to judge the end of the sentence,To stop the reverse order

 3、将字符串逆置

四、代码总结

五、The second method inverts the string

六、总结 


前言:

Today I brushed a question on Niuke.com,I feel quite meaningful,Now I will share it with you,And tell everyone how to solve the problem.

一、题目

事例:

输入下列字符: 

I like beijing.

输出的字符:

beijing. like I 

二、思路讲解 

1、Create an array to store the input string

2、Reverse each of these words 得到 I ekil .gnjieb

3、Reverse the string again  得到beijing.like I

三、代码实现

1.Create an array to store the input string

#include <stdio.h>
 
int main()
{
    char arr[100] = { 0 };
    gets(arr);//注意没有使用scanf,因为scanfWhen a space is encountered, it stops accepting input
 
    printf("%s\n",arr);
 
    return 0;
}

2、Invert each word 

这其中需要注意几点:

1.How the whole statement ends the loop

2.The start and end positions of each word

3.内部如何实现

4. 判断语句结束,Thereby stopping the reverse order

How the statement ends the loop:

	char* t = arr;//Put the address of the first element of the character array into a pointer variablet中
	while (*t!='\0') {  
//判断当*t指向'\0’时循环结束.
}

The start and end positions of each word :

char* t = arr;//Put the address of the first element of the character array into a pointer variablet中
	while (*t!='\0') {  //判断当*t指向'\0’时循环结束.
		char* start = t;
		char* end = t;
		  //When the end pointer points to a space,并且指向‘\0’循环结束.
		while (*end != ' '&&*end != '\0') {
			end++;
		}

内部怎么实现:

We need to define another function ourselvesreverse,Used to implement the exchange of strings

void reverse(char* left, char* right) {
	while (left < right) {//Only if the address on the left is less than the address on the right,才交换,The middle ones do not need to be exchanged
		char tmp = *right;
		*right = *left;
		*left = tmp;
		left++;
		right--;
	}
}

再调用这个函数:

int main() {
	char arr[100] = { 0 };
	gets(arr); //输入字符数组
	char* t = arr;//Put the address of the first element of the character array into a pointer variablet中
	while (*t!='\0') {  //判断当*t指向'\0’时循环结束.
		char* start = t;
		char* end = t;
		  //When the end pointer points to a space,并且指向‘\0’循环结束.
		while (*end != ' '&&*end != '\0') {
			end++;
		}
		reverse(start, end - 1);
	}

 How to judge the end of the sentence,To stop the reverse order

if (*end != '\0')//Check if the end of the string is reached
			t = end + 1;//让指针p指向下一个单词,Let the loop go to the next word
		else
			t = end;

 

 3、将字符串逆置

This is used to calculate the length of the stringstrlen函数,

int len = strlen(arr);

 再调用reverse函数:

reverse(arr, arr + len - 1);

四、代码总结

void reverse(char* left, char* right) {
	while (left < right) {
		char tmp = *right;
		*right = *left;
		*left = tmp;
		left++;
		right--;
	}
}
int main() {
	char arr[100] = { 0 };
	gets(arr); //输入字符数组
	char* t = arr;//Put the address of the first element of the character array into a pointer variablet中
	while (*t!='\0') {  //判断当*t指向'\0’时循环结束.
		char* start = t;
		char* end = t;
		  //When the end pointer points to a space,并且指向‘\0’循环结束.
		while (*end != ' '&&*end != '\0') {
			end++;
		}
		reverse(start, end - 1);
		if (*end != '\0')//Check if the end of the string is reached
			t = end + 1;//让指针p指向下一个单词,Let the loop go to the next word
		else
			t = end;
	}
	int len = strlen(arr);
	reverse(arr, arr + len - 1);
	printf("%s\n", arr);
	return 0;
}

五、The second method inverts the string

     The idea of ​​method 2 is to use it firstt指向字符串的末尾,然后往前走,找到空格时,printf一下,以%s格式,这样打印只需要给字符串的首地址,它打印到\0停下.我们打印完一个单词后,把t的位置变成\0,然后继续往前走,直到数组开始位置.

     t指针开始指向字符串最后一个字符,然后往前遍历,直到空格或者t到了最开始的字符位置,这里分两种情况,到空格位置,要Change the space position to \0,打印cur+1位置,到首字符,打印cur位置.

      其中需要注意一点,我们Change the space position to 了\0,所以在第一种情况需要打印的时候在加个空格
    

代码如下: 

int main()
{
    char arr[100] = { 0 };
    gets(arr);
    char* t = arr + strlen(arr) - 1;  //将tPointer to the last character of the string
    while (t > arr)    //当tThe address of the pointer is greater thanarrThe first element address starts the loop
    {
        while (*t != ' ' && t > arr)  //当t不为空格时,tThe pointer loops forward
        {
            t--;
        }

        if (t == arr)
        {
            printf("%s", t);
        }
        else //到空格,打印t+1位置
        {
            printf("%s ", t + 1);
            *t = '\0';   //Change the space position to ‘\0’
        }
    }
    return 0;
}

六、总结 

 今天的分享就到这了,The first method uses arrays,字符串逆置,容易想到,第二种方法,Difficulty thinking,代码简单,两种方法都可以,今天的分享就到这了,谢谢大家的支持.

原网站

版权声明
本文为[Code Chen Shuai]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208091906264402.html