您现在的位置是:网站首页> 编程资料编程资料
利用perl、python、php、shell、sed、awk、c 实现字符串的翻转_linux shell_
2023-05-26
364人已围观
简介 利用perl、python、php、shell、sed、awk、c 实现字符串的翻转_linux shell_
原题:
Q:有a.txt文件,里面内容如下 1234569 abcABCabc 要求使用awk打印出以下结果 987654321 cbaCBAcba
A:
shell :[root@vps tmp]# rev a.txt 9654321 cbaCBAcba
perl : [root@vps tmp]# perl -nle ‘print scalar reverse $_;' a.txt 9654321 cbaCBAcba
awk: [root@vps tmp]# awk ‘{num=split($0,arr,”");for(i=num;i>0;i–){printf arr[i];if(i==1){printf “\n”}}}' a.txt 9654321 cbaCBAcba
php: [root@vps tmp]# php -r ‘$fp=fopen(“a.txt”,”r”);while(!feof($fp)){ echo strrev(fgets($fp,999));} echo “\n”;;' 9654321 cbaCBAcba
sed: [root@vps tmp]# sed ‘/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//' a.txt 9654321 cbaCBAcba
python: (方法之一)
>>> arr='hello'
>>> arr[::-1]
‘olleh'
(方法二)
>>> str='hello'
>>> tmp=”
>>> for s in reversed(str):
… tmp+=s
…
>>> print tmp
olleh
reverse.h
#ifndef _reverse_h
int getLine(char s[],int limit);
void reverse(char s[]);
#endif
int getLine(char s[],int limit)
{
int c,i ;
for(i =0; ireverse.c
#include "stdio.h"
#include "/Users/chenqing/tmp/reverse.h"
#define MAXLINE 1000
/*
reverse a string use c langusge
*/
int main(void)
{
int len;
char line[MAXLINE];
if((len = getLine(line,MAXLINE)) > 0){
reverse(line);
printf("%s",line);
}
return 0;
}
gcc reverse.c -o reverse
Qing:tmp chenqing$ echo "ChinaCache" |./reverse
ehcaCanihC上面就是利用这些实现我们在终端命令行下实现字符串翻转的例子,哈哈,其实还是perl实现起来比较爽啊。
相关内容
- 在shell或者perl中改变字体或背景的颜色_linux shell_
- shell脚本函数:控制颜色、定位、居中显示的代码_linux shell_
- linux BASH shell下设置字体及背景颜色_linux shell_
- shell脚本正则匹配文件中的Email并写入到文件中代码分享_linux shell_
- shell脚本递归遍历目录及子目录的例子分享_linux shell_
- shell按行读取文件的3种方法_linux shell_
- shell脚本中执行时提示“没有那个文件或目录”的解决办法_linux shell_
- linux shell命令行选项与参数用法详解_linux shell_
- 把文件复制N份的2个Shell脚本代码_linux shell_
- Shell脚本批量修改文件后缀名代码分享_linux shell_
