当前位置:网站首页>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 .
边栏推荐
- NETCORE performance troubleshooting
- Get to know Prometheus
- 有关计网的五种类型题
- Houdini图文笔记:Could not create OpenCL device of type (HOUDINI_OCL_DEVICETYPE)问题的解决
- Google Earth Engine(GEE)——evaluate实现一键批量下载研究区内的所有单张影像(上海市部分区域)
- MCU development -- face recognition application based on esp32-cam
- 报名开启|飞桨黑客马拉松第三期如约而至,久等啦
- tokenizers>=0.11.1,!=0.11.3,<0.13 is required for a normal functioning of this module,
- The left sliding menu +menu item icon is grayed out
- Unreal Engine graphics and text notes: use VAT (vertex animation texture) to make Houdini end on Houdini special effect (ue4/ue5)
猜你喜欢

Basic use and principle of Minio

我希望按照我的思路盡可能將canvas基礎講明白

Network protocol learning -- lldp protocol learning

【论文阅读|深度】Role-based network embedding via structural features reconstruction with degree-regularized

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

Modbus protocol and serialport port read / write

【论文阅读|深读】LINE: Large-scale Information Network Embedding

Es learning

戴尔科技演绎“快”字诀,玩转CI/CD

Flask blog practice - archiving and labeling of sidebar articles
随机推荐
Performance memory
Network protocol learning -- lldp protocol learning
我的作文题目是——《我的区长父亲》
XSS attack
新学派:不诈骗经济学
单片机进阶---PCB开发之照葫芦画瓢(二)
炒股票开户的话,手机开户安全吗?有谁知道啊?
一文了解Prometheus
Houdini graphic notes: could not create OpenCL device of type (houdini_ocl_devicetype) problem solving
【动态规划】—— 数字三角形
QT: parsing JSON
Unreal Engine graphics and text notes: use VAT (vertex animation texture) to make Houdini end on Houdini special effect (ue4/ue5)
Growth: how to think deeply and learn
tokenizers>=0.11.1,!=0.11.3,<0.13 is required for a normal functioning of this module,
The left sliding menu +menu item icon is grayed out
Flask博客实战 - 实现侧边栏文章归档及标签
Opencv learning (II) -- installing opencv on raspberry pie
ES 学习
Flask博客实战 - 实现个人中心及权限管理
【论文阅读|深读】LINE: Large-scale Information Network Embedding