当前位置:网站首页>Previous string inversion topic
Previous string inversion topic
2022-06-25 10:47:00 【Embedded Linux,】
The previous issue of string inversion
The problem of string inversion , Can you think of a better way ?
A lot of people commented , Some people also wrote their own ideas for solving problems , Others have written their own code

There is also a very popular stack pressing solution

I believe that many people will encounter this kind of problem in the written examination , Give you a string , Let you find some rules , Or find a string , Or character case conversion .
Let's take a look first , If we use the stack to complete this code, how to write ?
The answer I posted above actually uses the idea of stack , Queue first in first out , Stack means first in and last out .
Stack ,C Language implementation
So the code above

In the form of stack , The last position comes out first .
What if my code is in stack form ? I wrote a rough version
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
char input[] = {"the sky is blue"};
// subject :
//input the sky is blue
//output blue is sky the
void reverseWords(char* s, size_t n) {
char *stack = (char*)malloc(n);
memcpy(stack, s, n);
for (int i=0; i<n; i++) {
*(s + i) = *(stack + n -i -1);
}
if (stack) {
free(stack);
stack = NULL;
}
}
//eulb si yks eht
void reverseWords_by_space(char* s, int n) {
int i = 0;
int len = 0;
for (i=0; i<n; i++) {
if (s[i] == ' ') {
reverseWords(s+i-len, len);
len = 0;
} else if (s[i] == '\0') {
reverseWords(s+i-len, len);
len = 0;
}else {
++len;
}
}
}
int main(void) {
printf("%s\n", input);
reverseWords(input,strlen(input));
reverseWords_by_space(input,sizeof(input));
printf("%s\n", input);
// Finished writing , If you have any comments you don't understand
return 0;
} But I don't think it's very good , Because it uses Memory application , Embedded should know , Memory is a scarce resource for us .
So I still think the above student's writing is very awesome
Some people reply that XOR is used to exchange two variables , There are many ways to exchange variables , But sometimes I can't remember during the interview , So we will write the simplest way , But there are some common ways you can try .
#include "stdio.h"
void swap4(int *a,int *b) {
*a = (*a + *b) - (*b = *a);
}
void swap3(int *a,int *b) {
*a = (*a ^ *b) ^ (*b = *a);
}
void swap2(int *a,int *b) {
*a = *a + *b;
*b = *a - *b;
*a = *a - *b;
}
void swap1(int *a,int *b) {
*a = *a^*b;
*b = *a^*b;
*a = *a^*b;
}
int main(void) {
int a = 3,b = 4;
printf("a=%d,b=%d\n",a,b);
swap1(&a,&b);
printf("a=%d,b=%d\n",a,b);
swap2(&a,&b);
printf("a=%d,b=%d\n",a,b);
swap3(&a,&b);
printf("a=%d,b=%d\n",a,b);
swap4(&a,&b);
printf("a=%d,b=%d\n",a,b);
return 0;
} Output 
I want to update the code again in the evening #include "stdio.h"
#include "string.h"
#include "stdlib.h"
char input[] = {"the sky is blue cris 1212321 apple"};
// subject :
//input the sky is blue
//output blue is sky the
void reverseWords(char* s, size_t n) {
*(s+n-1) = '\0';
printf("%s ",s);
}
int main(void) {
int size = sizeof(input);
printf("%s\n",input);
for (int i=0,n=0; i<=size; i++,n++) {
if (*(input+size-i-1) == ' ' || i == size){
reverseWords(input+size-i, n);
n = 0;
}
}
return 0;
} If there is a better way , Welcome to leave a message . If you see a written test with a variant of this question , Also welcome to leave a message .
边栏推荐
猜你喜欢

什么是 CRA

Es learning

Dell technology performs the "fast" formula and plays ci/cd

NETCORE performance troubleshooting

How to install SSL certificates in Microsoft Exchange 2010

Modbus protocol and serialport port read / write

西门子PLCS7-200使用(一)---开发环境和组态软件入门

网络远程访问的方式使用树莓派

看完这篇 教你玩转渗透测试靶机Vulnhub——DriftingBlues-7

【论文阅读|深度】Role-based network embedding via structural features reconstruction with degree-regularized
随机推荐
Your driver settings have been set to force 4x antialiasing in OpenGL applications
软件测试 避免“试用期被辞退“指南,看这一篇就够了
我希望按照我的思路盡可能將canvas基礎講明白
每日3题(3)-检查整数及其两倍数是否存在
我希望按照我的思路尽可能将canvas基础讲明白
Yolov5 changing the upper sampling mode
数组结构整理
Create menu file
Performance network
[dynamic planning] - Digital triangle
Yolov5更换上采样方式
Houdini图文笔记:Your driver settings have been set to force 4x Antialiasing in OpenGL applications问题的解决
性能之网络篇
Learn to learn self-study [learning to learn itself is more important than learning anything]
一个五年北漂的技术er,根据这些年的真实经历,给应届生的一些建议
Requirements and precautions for applying for multi domain SSL certificate
XSS attack
Is it safe to speculate in stocks by mobile phone?
服务端渲染
Is it safe to open an account with Guangzhou securities by mobile phone?