日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C語言如何實現翻轉字符串中的單詞_C 語言

作者:Futureing ? 更新時間: 2022-09-13 編程語言

C語言翻轉字符串中的單詞

另外開辟一個空間,來存放翻轉的字符串

單詞之間是以空格間隔的,所以我們翻轉需要一個一個字符進行翻轉,我們需要找尋空格,找到空格表示一個字符已經找到,進行以下的步驟:

1. 首先獲取原字符串的長度,申請一個長度+1的空間,因為還需要一個結束符。

2. 定義一個變量i,初始化為0,用i進行字符串的遍歷,定義一個start用來表示每個單詞的起始位置。

3. 遍歷字符串,不是空格i++,直到遇見空格,表示找到了一個單詞。

4. 將該單詞從末尾放到咱們新開辟的數組中,就實現了逆序。

5. 空格也需要放到臨時數組中保持與原數組一致。

#include<stdio.h>
#include<string.h>
char * reverseWords(char * s);
int main(){
?? ?char s[] = "Let's pass the examination";
?? ?char *temp = reverseWords(s);
?? ?printf("%s",temp);
?? ?return 0;
}?
char * reverseWords(char * s){
?? ?// 獲取該字符串長度?
?? ?int length = strlen(s);
?? ?// 申請新的空間?
?? ?char* temp = (char*)malloc(sizeof(char)*(length+1));
?? ?// 結束符\0?
?? ?temp[length] = 0;
?? ?int i = 0;
?? ?while(i<length){
?? ??? ?// 單詞的起始位置?
?? ??? ?int start = i;
?? ??? ?// 找尋單詞,因為字符串中的單詞以空格間隔?
?? ??? ?while(i<length&&s[i]!=' '){
?? ??? ??? ?i++;
?? ??? ?}
?? ??? ?// 找到了一個單詞進行交換,將后邊的字符放到前邊?
?? ??? ?for(int j=start;j<i;j++){
?? ??? ??? ?// 將單詞從后往前放置到新的數組中實現單詞的逆序?
?? ??? ??? ?temp[j] = s[start+i-1-j];
?? ??? ?}
?? ??? ?// 處理空格?
?? ??? ?while(i<length&&s[i]==' '){
?? ??? ??? ?temp[i] = ' ';
?? ??? ??? ?i++;
?? ??? ?}
?? ?}?
?? ?return temp;?
}

直接在原數組上進行操作

使用雙指針進行單詞的翻轉,一個指向單詞的開始,一個指向單詞的末尾。與上述方方法幾乎一致,只是交換的時候發生了一些變化。

1. 先通過空格找尋單詞。

2. 然后通過雙指針實現單詞翻轉。

3. 繼續遍歷字符串,空格跳過。

4. 完成整個字符串的翻轉。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char * reverseWords(char * s);
int main(){
?? ?char s[] = "Let's pass the examination";
?? ?char *temp = reverseWords(s);
?? ?printf("%s",temp);
?? ?return 0;
}?
char * reverseWords(char * s){
?? ?// 獲取該字符串長度?
?? ?int length = strlen(s);
?? ?int i = 0;
?? ?while(i<length){
?? ??? ?// 單詞的起始位置?
?? ??? ?int start = i;
?? ??? ?// 找尋單詞,因為字符串中的單詞以空格間隔?
?? ??? ?while(i<length&&s[i]!=' '){
?? ??? ??? ?i++;
?? ??? ?}
?? ??? ?// 雙指針?
?? ??? ?int left = start;
?? ??? ?int right = i-1;?
?? ??? ?// 雙指針法進行翻轉?
?? ??? ?while(left<right){
?? ??? ??? ?char temStr = s[left];
?? ??? ??? ?s[left] = s[right];
?? ??? ??? ?s[right] = temStr;
?? ??? ??? ?left++;
?? ??? ??? ?right--;
?? ??? ?}
?? ??? ?// 空格忽略?
?? ??? ?while (i<length && s[i]==' ') {
? ? ? ? ? ? i++;
? ? ? ? }
?? ?}?
?? ?return s;?
}

C語言字符串各單詞的反轉

思路

我們首先把字符串整個反轉,然后在逐個單詞反轉回來,每個單詞通過空格' '分隔開

代碼實現

int strReverse(char * head, char * tail){
	if(NULL == head || NULL == tail){
		printf("strReverse param error!\n");
		return PARAM_ERR;
	}
	
	char tmp;
 
	while(head < tail){
		tmp = *head;
		*head = *tail;
		*tail = tmp;
 
		head++;
		tail--;
	}
	
	return SUCCESS;
}
 
int strReversWithoutWord (char * str){
	if(NULL == str){
		printf("strReverse param error!\n");
		return PARAM_ERR;
	}
 
	int len = 0;
	char * head = NULL, * tail = NULL;
	char * p = NULL, * q = NULL;
 
	/*1. 整個字符串顛倒*/
	len  = strlen(str);
	head = str;
	tail = str + len - 1;
	strReverse(head, tail);
	
	/*逐個單詞顛倒回來*/
	p = str;
	while(*p != '\0'){
		head = p;
		q = p;
		while(' ' != *q && '\0' != *q){
			q++;
		}
		tail = q - 1;
		strReverse(head, tail);
		
		p = q + 1;
	}
}
 
void testReverse(void){
	char str[100] = "Feng is from Beijing!";
	char * head = NULL, * tail = NULL;
	int len  = 0;
 
	printf("\n************  testReverse ************ \n");
 
	strReversWithoutWord(str);
	printf("Reverse string without word is : %s\n\n", str);
 
	return;
}

代碼編譯

gcc main.c str.c -g -o a.exe

調試輸出

************ ?testReverse ************
?
Reverse string without word is : Beijing! from is Feng

原文鏈接:https://blog.csdn.net/Futureing/article/details/124436420

欄目分類
最近更新