当前位置:网站首页>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 .
边栏推荐
- 【历史上的今天】6 月 24 日:网易成立;首届消费电子展召开;世界上第一次网络直播
- 西门子PLCS7-200使用(一)---开发环境和组态软件入门
- Output reading: apply what you have learned
- Is it safe to open an account through mobile phone if you open an account through stock speculation? Who knows?
- 之前字符串反转的题目
- Google Earth Engine (Gee) - evaluate réalise le téléchargement en un clic de toutes les images individuelles dans la zone d'étude (certaines parties de Shanghai)
- XSS攻击
- The title of my composition is - "my district head father"
- [image fusion] image fusion based on morphological analysis and sparse representation with matlab code
- [paper reading | deep reading] line: large scale information network embedding
猜你喜欢
随机推荐
Garbage collection mechanism
XSS攻击
手机炒股安全吗?
Binder explanation of Android interview notes
Flask blog practice - realize the latest articles and search in the sidebar
QT: parsing JSON
Think about it
Unreal Engine graphics and text notes: use VAT (vertex animation texture) to make Houdini end on Houdini special effect (ue4/ue5)
Array structure collation
【观察】ObjectScale:重新定义下一代对象存储,戴尔科技的重构与创新
FPGA基于VGA显示字符及图片
Your driver settings have been set to force 4x antialiasing in OpenGL applications
Is it safe to open a stock account on the compass?
Five types of questions about network planning
【论文阅读|深读】DRNE:Deep Recursive Network Embedding with Regular Equivalence
Opencv learning (I) -- environment building
June 24, 2022: golang multiple choice question, what does the following golang code output? A:1; B:3; C:4; D: Compilation failed. package main import ( “fmt“ ) func mai
持续交付-Jenkinsfile 语法
如何在Microsoft Exchange 2010中安装SSL证书
戴尔科技演绎“快”字诀,玩转CI/CD









