日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

C函数加密实现及常用字符串处理函数的使用

發布時間:2025/3/15 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C函数加密实现及常用字符串处理函数的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
getpass() getpass()函數用于從控制臺輸入一行字符串,關閉了回顯(輸入時不顯示輸入的字符串),適用于用密碼的輸入。 語法 char * getpass (const char * prompt); 參數prompt為提示字符串地址。 getpass()函數返回值:輸入字符串地址。#include #include int main(void) { char *password; password = getpass("Input a password:"); /*輸入密碼*/ printf("The password is: %s/r/n", password); /*顯示密碼*/ return 0; } getpass函數通常會與crypt加密函數一同使用 #include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <sys/types.h> #include <sys/wait.h> #include <crypt.h> int main() { char passwd[13]; char *key; char slat[2]; key = getpass("Input first passward:"); slat[0] = key[0]; slat[1] = key[1]; strcpy(passwd,crypt(key,slat)); key = getpass("Input second passward:"); slat[0] = passwd[0]; slat[1] = passwd[1]; printf("After crypt(),1st passwd:%s\n",passwd); printf("After crypt(),2st passwd:%s\n",passwd); return 0; }

realpath函數

realpath是用來將參數path所指的相對路徑轉換成絕對路徑,然后存于參數resolved_path所指的字符串數組或指針中的一個函數。 如果resolved_path為NULL,則該函數調用malloc分配一塊大小為PATH_MAX的內存來存放解析出來的絕對路徑,并返回指向這塊區域的指針。程序員應調用free來手動釋放這塊內存 #include <stdlib.h> 函數原型:char *realpath(const char *path, char *resolved_path) 返回值: 成功則返回指向resolved_path的指針,失敗返回NULL,錯誤代碼存于errno 范例: #include <unistd.h> main() {char resolved_path[80];realpath("/usr/X11R6/lib/modules/../../include/../",resolved_path);printf("resolved_path: %s\n", resolved_path); }

getopt函數
getopt(分析命令行參數)
相關函數
表頭文件 #include

#include <stdio.h>#include<unistd.h>int main(int argc, char *argv[]){int ch;opterr = 0;while((ch = getopt(argc,argv,"a:bcde"))!= -1){switch(ch){case : printf("xxxtest");case 'a': printf("option a:’%s’\n",optarg); break;case 'b': printf("option b :b\n"); break;default: printf("other option :%c\n",ch);}printf("optopt +%c\n",optopt);}return 0;} 執行 $./getopt –b option b:b 執行 $./getopt –c other option:c 執行 $./getopt –a other option :? 執行 $./getopt –a12345 option a:’12345

神奇的字符分割函數strtok()函數

例子1:

#include<string.h> #include<stdio.h> int main(void) {char input[16]="abc,d";char*p;/*strtok places a NULL terminatorinfront of the token,if found*/p=strtok(input,",");if(p)printf("%s\n",p);/*Asecond call to strtok using a NULLas the first parameter returns a pointerto the character following the token*/p=strtok(NULL,",");if(p)printf("%s\n",p); return 0; }

例子2:

#include<string.h> #include<stdio.h> int main(void) {char input[16]="abc,123,e,f";char*p;/*strtok places a NULL terminatorinfront of the token,if found*/p=strtok(input,",");if(p)printf("%s\n",p);/*Asecond call to strtok using a NULLas the first parameter returns a pointerto the character following the token*/p=strtok(NULL,",");if(p)printf("%s\n",p);p=strtok(NULL,",");if(p)printf("%s\n",p);p=strtok(NULL,",");if(p)printf("%s\n",p);return 0; }

輸出:

/home/andrew/文檔/untitled5/cmake-build-debug/untitled5 abc 123 e fProcess finished with exit code 0

C++例子:

#include<iostream> #include<cstring> using namespace std; int main() {char sentence[]="This is a sentence with 7 tokens";cout << "The string to be tokenized is:\n" << sentence << "\n\nThe tokens are:\n\n";char *tokenPtr=strtok(sentence," ");while(tokenPtr!=NULL) {cout<<tokenPtr<<'\n';tokenPtr=strtok(NULL," ");}//cout << "After strtok,sentence=" << tokenPtr<<endl;return 0; }

strtod函數

strtod是C語言及C++中的重要函數,功能是將字符串轉換成浮點數,表頭文件是#include

相關 atoi,atol,strtod,strtol,strtoul 表頭文件 #include <stdlib.h> 定義函數 double strtod(const char *nptr,char **endptr);

strtod()會掃描參數nptr字符串,跳過前面的空格字符,直到遇上數字或正負符號才開始做轉換,到出現非數字或字符串結束時(‘\0’)才結束轉換,并將結果返回。若endptr不為NULL,則會將遇到不合條件而終止的nptr中的字符指針由endptr傳回。參數nptr字符串可包含正負號、小數點或E(e)來表示指數部分。如123.456或123e-2。

小測試

#include<stdlib.h> #include<stdio.h> void main() {char *endptr;char a[] = "asd12345.6789";char b[] = "1234.567qwer";char c[] = "-232.23e4";printf( "a=%lf\n", strtod(a+3,NULL) );printf( "b=%lf\n", strtod(b,&endptr) );printf( "endptr=%s\n", endptr );printf( "c=%lf\n", strtod(c,NULL) ); }

輸出

/home/andrew/文檔/untitled5/cmake-build-debug/untitled5 a=12345.678900 b=1234.567000 endptr=qwer c=-2322300.000000Process finished with exit code 0

atoi函數

**
atoi (表示 ascii to integer)是把字符串轉換成整型數的一個函數,應用在計算機程序和辦公軟件中。atoi( ) 函數會掃描參數 nptr字符串,跳過前面的空白字符(例如空格,tab縮進等。
可以通過isspace( )函數來檢測),直到遇上數字或正負符號才開始做轉換,而在遇到非數字或字符串結束時(‘\0’)才結束轉換,并將結果返回。如果 nptr不能轉換成 int 或者 nptr為空字符串,那么將返回 0[1] 。
**

//vs2013里調用printf函數請使用預處理命令#define _CRT_SECURE_NO_WARNINGS #include <stdlib.h> #include <stdio.h>int main(void) {int n;char *str = "12345.67";n = atoi(str);printf("n=%d\n",n);return 0; } 輸出: n = 12345 //vs2013里調用printf函數請使用預處理命令#define _CRT_SECURE_NO_WARNINGS #include <stdlib.h> #include <stdio.h>int main() {char a[] = "-100";char b[] = "123";int c;c = atoi(a) + atoi(b);printf("c=%d\n", c);return 0; } 執行結果: c = 23

atol函數

**
函數名: atol
功 能: 把字符串轉換成長整型數
用 法: long atol(const char *nptr);
**

表頭文件: #include

#include <stdlib.h> #include <stdio.h> int main(void) {long l;char *str = "98765432";l = atol(str); /* 原來為l = atol(str); */printf("string = %s integer = %ld\n", str, l);return(0); } 執行 string=98765432 integer=98765432 #include<stdlib.h> main() {char a[]=”1000000000”;char b[]=” 234567890”;long c;c=atol(a)+atol(b);printf(“c=%d\n”,c); } 執行 c=1234567890

## isdigit()函數 ##
isdigit是計算機C(C++)語言中的一個函數,主要用于檢查參數是否為十進制數字字符。用于判斷字符c是否為數字,當c為數字0~9時,返回非零值,否則返回零。 可以用一個字符數組循環判斷每一項是否為數字

/* 找出字符數組str中為阿拉伯數字0~9的字符*/ #include <stdio.h> #include <stdlib.h> #include <ctype.h>int main(){char str[]="1776ad";int year;printf ("The year that followed %d was %d.\n", year, year+1); }return 0;}

## 解開strtoul的神秘面紗 ##
strtoul,將參數nptr字符串根據參數base來轉換成無符號的長整型數。
strtoul (將字符串轉換成無符號長整型數)
相關函數
atof,atoi,atol,strtod,strtol
unsigned long strtoul(const char *nptr,char **endptr,int base);
【參數說明】str 為要轉換的字符串,endstr 為第一個不能轉換的字符的指針,base 為字符串 str 所采用的進制。
【函數說明】strtoul() 會將參數 str 字符串根據參數 base 來轉換成無符號的長整型數(unsigned long)。參數 base 范圍從2 至36,或0。參數 base 代表 str 采用的進制方式,如 base 值為10 則采用10 進制,若 base 值為16 則采用16 進制數等。
溫馨提示:ANSI C 規范定義了 stof()、atoi()、atol()、strtod()、strtol()、strtoul() 共6個可以將字符串轉換為數字的函數,大家可以對比學習。另外在 C99 / C++11 規范中又新增了5個函數,分別是 atoll()、strtof()、strtold()、strtoll()、strtoull()
**參數1:字符串起始地址
參數2:返回字符串有效數字的結束地址,這也是為什么要用二級指針的原因。
參數3:轉換基數。當base=0,自動判斷字符串的類型,并按10進制輸出,例如”0xa”,就會把字符串當做16進制處理,輸出的為10。更多的下面詳細闡述。**

strtoul()會將參數nptr字符串根據參數base來轉換成無符號的長整型數。參數base范圍從2至36,或0。參數base代表采用的進制方式,如base值為10則采用10進制,若base值為16則采用16進制數等。當base值為0時會根據情況選擇用哪種進制:如果第一個字符是’0’,就判斷第二字符如果是‘x’則用16進制,否則用8進制;第一個字符不是‘0’,則用10進制。一開始strtoul()會掃描參數nptr字符串,跳過前面的空格字符串,直到遇上數字或正負符號才開始做轉換,再遇到非數字或字符串結束時(”)結束轉換,并將結果返回。若參數endptr不為NULL,則會將遇到不合條件而終止的nptr中的字符指針由endptr返回。
返回值
返回轉換后的長整型數,否則返回ERANGE并將錯誤代碼存入errno中。
附加說明
ERANGE指定的轉換字符串超出合法范圍。

#include <stdio.h> #include <stdlib.h> int main () {char buffer [256];unsigned long ul;printf ("Enter an unsigned number: ");fgets (buffer, 256, stdin);ul = strtoul (buffer, NULL, 0);printf ("Value entered: %lu.\n", ul);system("pause");return 0; }

例子中間變量是存放第一個不能轉換的變量的地址

#include <stdio.h> #include <stdlib.h> int main () {char szNumbers[] = "2001 60c0c0 -1101110100110100100000 0x6fffff";char * pEnd;long int li1, li2, li3, li4;li1 = strtol (szNumbers,&pEnd,10);li2 = strtol (pEnd,&pEnd,16);li3 = strtol (pEnd,&pEnd,2);li4 = strtol (pEnd,NULL,0);printf ("轉換成10進制: %ld、%ld、%ld、%ld\n", li1, li2, li3, li4);return 0; }

輸出結果:

/home/andrew/文檔/untitled5/cmake-build-debug/untitled5 轉換成10進制: 2001、6340800、-3624224、7340031Process finished with exit code 0

總結

以上是生活随笔為你收集整理的C函数加密实现及常用字符串处理函数的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。