天天看點

c/c++整理--字元串(4)一、将十進制數轉化為二進制和十六進制形式輸出二、程式設計實作轉換字元串、插入字元的個數三、字元串編碼問題

一、将十進制數轉化為二進制和十六進制形式輸出

不使用printf系列庫函數,通過位運算将十進制數以二進制和十六進制形式的字元串輸出。

#include <stdio.h>
#include <stdlib.h>

#define N 32
#define M 10

char sel[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};

char* getstr2(const int num)
{
	char *str2 = (char*)malloc(sizeof(char)*N);
	int temp = num, i = 0;
	for(i = 0; i < N; i++)
	{
		str2[i] = '0';
	}
	i = N-1;
	while(temp)
	{
		str2[i] = temp % 2 + 48;
		i--;
		temp = temp / 2;
	}
	return str2;
}

char* getstr16(const int num)
{
	char* str16 = (char*)malloc(sizeof(char)*M);
	int temp = num, i = 0;
	for(i = 0; i < M; i++)
	{
		str16[i] = '0';
	}
	str16[1] = 'x';
	i = M - 1;
	while(temp)
	{
		str16[i] = sel[temp % 16];
		i--;
		temp = temp / 16;
	}
	return str16;
}

int main()
{
	int num;
	char* str2;
	char* str16;
	printf("輸入一個十進制數:");
	scanf("%d", &num);
	
	str2 = getstr2(num);
	str16 = getstr16(num);
	
	printf("其二進制為:%s\n", str2);
	printf("其十六進制為:%s\n", str16);
	
	return 0;
}
           

這道題很簡單,隻要知道二進制、十進制和十六進制之間的轉換規律就可以了,int型資料在32位系統中占4個位元組,即32位,是以二進制輸出數組應配置設定32個位元組,十六進制的轉換要注意是以0x開頭的。 輸出:

輸入一個十進制數:15536
其二進制為:00000000000000000011110010110000
其十六進制為:0x00003CB0
           

二、程式設計實作轉換字元串、插入字元的個數

據題意,需要在字元串中插入字元統計的個數,例如字元串aaab,插入字元個數後為:aaa3b1.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N 100

char* transformation(char* str)
{
	int len = strlen(str);
	char *buf = (char*)malloc(sizeof(char)*(len+1));
	
	char *p = str;
	char *q = p+1;
	int count = 1;
	
	while(*q)
	{
		if(*p == *q)
		{
			count++;
			p++;
			q++;
		}
		else
		{			
			itoa(count, buf, 10);
			int nbits = strlen(buf);
			strcat(buf, q);
			*q = '\0';
			strcat(str, buf);
			q+=nbits;
			p = q;
			q = p+1;
			count = 1;
		}
	}
	
	itoa(count, buf, 10);
	strcat(str, buf);
	
	free(buf);
	buf = NULL;
	
	return str;
}

int main()
{
	char str[N];
	printf("please input:");
	scanf("%s", str);
	printf("before transformation:%s\n", str);
	char *pstr = transformation(str);
	printf("after transformation:%s\n", pstr);
	
	return 0;
}

           

這個程式中,transformation函數用來轉換字元串,以“aaab”為例,首先申請5個位元組的堆記憶體buf來存放字元串數字相關資訊。初始計數為1,周遊aaab,知道遇到不同的字元,在buf中儲存3b,把str變為aaa,然後兩者連接配接,變為aaa3b,一次類推,依次循環。

輸出:

please input:aaab
before transformation:aaab
after transformation:aaa3b1
           

三、字元串編碼問題

對于一個長度小于20的字元串進行編碼,遵循規則: (1)把字元串中的字母換成之後的第四個字母,例 a->e,A->E,X->b,y->c,z->d (2)如果不是字母,不替換 (3)翻轉整個字元串

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char arr[] = {'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd'};

void change(char* str)
{
	int len = strlen(str);
	int i = 0;
	for(i = 0; i < len/2; i++)
	{
		char temp = str[i];
		str[i] = str[len-i-1];
		str[len-i-1] = temp;
	}
}

void getfour(char* str)
{
	int i = 0;
	while(*str)
	{
		if((*str >= 'a' && *str <= 'v') || (*str >= 'A' && *str <= 'V'))
		{
			*str += 4;
			str++;
			continue;
		}
		for(i = 0; i < 16; i++)
		{
			if(*str == arr[i])
			{
				*str = arr[i+4];
				break;
			}
		}
		str++;
	}
}

int main()
{
	char hello[] = "hasd11H";
	getfour(hello);
	change(hello);
	printf("%s\n", hello);
	
	return 0;
}
           

程式中change函數實作了字元串的倒轉,getfour函數實作了字元從編碼,如果字元是‘a’—‘v’或者‘A’—‘V’,其值加上4就可以了,但是最後的四個字母及其大寫要注意,我将之放在一個全局數組裡面,隻需對數組進行周遊即可。

輸出:

L11hwel
           

繼續閱讀