利用perl、python、php、shell、sed、awk、c 實現(xiàn)字符串的翻轉(zhuǎn)
原題:
Q:有a.txt文件,里面內(nèi)容如下 1234569 abcABCabc 要求使用awk打印出以下結(jié)果 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上面就是利用這些實現(xiàn)我們在終端命令行下實現(xiàn)字符串翻轉(zhuǎn)的例子,哈哈,其實還是perl實現(xiàn)起來比較爽啊。
相關(guān)文章
shell腳本實現(xiàn)同時多臺遠程主機執(zhí)行命令的代碼分享
這篇文章主要給大家介紹了關(guān)于shell腳本實現(xiàn)同時多臺遠程主機執(zhí)行命令的方法,文中給出了詳細的代碼示例,相信對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-03-03
shell腳本發(fā)送http請求的實現(xiàn)示例
本文主要介紹了shell腳本發(fā)送http請求的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-04-04
詳解systemctl?和?service?區(qū)別及命令
systemctl和service都是管理Linux系統(tǒng)服務的工具,但systemctl更加先進,可以方便地管理systemd服務,而service適用于管理傳統(tǒng)的SysV服務,這篇文章主要介紹了systemctl和service區(qū)別及命令,需要的朋友可以參考下2023-07-07

