1,統計一個字符串中個字符的百分比:
如輸入字符串asdfs
a —– 20%
s —– 40%
d —–20%
f —–20%
#include <stdio.h>
#include <string.h>
void percentage( char *str, int len )
{
int i = 0;
int j = 0;
int count = 1;
int num = 0;
while( *str != '\0' )
{
for( i = 1; i < len; i++ )
{
while( *str == *(str + i) )
{
count++; //當後面的字符串中有相同字符時,count+1
*(str + i) = ' '; //同時讓相同的字符爲' '
}
}
if(*str != ' ')
{
num = count * 100 / len; //不好直接輸出百分比,讓num等於百分比之前的數字,百分號用printf輸出
printf("%c-------%d\%\n",*str,num); //輸出字符和相應的佔字符串中的百分比
count = 1; //讓count又=1
}
str++; //判斷下一個字符
}
}
int main()
{
char str[100] = {0};
int len = 0;
printf("please input astring:");
scanf("%s",str); //輸入字符串
len = strlen(str); //定義len爲字符串的長度
percentage(str,len); //調用percentage函數
return 0;
}
2、給一個字符串,有大小寫字母,要求寫一個函數把小寫字母放在前面,大寫字母放在後面,儘量使用最小空間,時間複雜度。(即用指針做)。
如:aAbBcCdD —abcdABCD
#include <stdio.h>
#include <string.h>
void str_sort(char *str,char *stra,char *strA)
{
while(*str != '\0')
{
if(*str >= 'a' && *str <= 'z')
{
*stra = *str;
stra++;
str++;
} //當字符爲小寫字母時將字符放進stra字符數組中
if(*str >= 'A' && *str <= 'Z')
{
*strA = *str;
strA++;
str++;
} //當字符爲大寫字母時將字符放進strA字符數組中
}
}
int main()
{
char str[100] = {0};
char stra[100] = {0};
char strA[100] = {0};
printf("please input a string:");
scanf("%s",str); //輸入一段含有大寫字母和小寫字母的字符串
str_sort(str,stra,strA); //調用str_sort函數
printf("%s\n",strcat(stra,strA)); //輸出stra、strA連接後的字符串
return 0;
}
3、自我實現atoi(字符串轉整形)
如:“123”轉換成 123
“-123” 轉換成 -123
#include <stdio.h>
#include <string.h>
int convert(char *str)
{
int len = strlen(str);
int i = 0;
int num = 0;
int temp = 1;
if( str[0] == '-' )
{
temp = -1; //判斷第一個字符是否爲-,是讓temp=-1
}
else
{
num = str[0] - '0'; //不是,讓其轉換爲數字並賦給num
}
for(i = 1; i < len; i++)
{
num = num * 10 + *(str + i) - '0'; //將字符轉換成數字
}
num = num * temp; //讓num乘以temp,正數轉換爲正數,負數轉換爲負數
return num; //返回num的值
}
int main()
{
char str[100] = {0};
int num = 0;
printf("please input a string:");
scanf("%s",str); //輸入數字字符串
num = convert(str); //調用轉換函數
printf("%d\n",num); //輸出轉換後的數字
return 0;
}
4、自我實現itoa(整形轉字符串)
如: 123 轉換成 “123”
“-123” 轉換成 -123
#include <stdio.h>
void convert(int num, char *str)
{
int i = 0;
int j = 0;
while( num != 0 )
{
if(num < 0)
{
num = -num; //如果num < 0,將其變成正數
}
i = num % 10 + '0';
str[j++] = i;
num /= 10; //從最低位開始將num中每一位取出加上'0'變成數字字符並存到str數組中
}
while(j--)
{
printf("%c",str[j]); //倒着輸出str數組中的字符
}
printf("\n");
}
int main()
{
char str[100] = {0};
int num = 0;
printf("please input a number:");
scanf("%d",&num); //輸入num的值
if(num < 0)
{
printf("-"); //如果num < 0,輸出一個'-'
}
convert(num, str); //調用轉換函數
return 0;
}
5、統計字符串中子串的個數。
如: 主串“aqwerqwerqwer”
子串 “qwer”
輸出 3
#include <stdio.h>
void compare(char *str1, char *str2)
{
int count = 0;
char *p1 = NULL;
char *p2 = NULL; //定義p1 p2兩個空指針
p1 = str1;
p2 = str2; //將str1和str2兩個字符串分別賦給p1 p2
while(*p1 != '\0')
{
if(*p1 == *p2)
{
p1++;
p2++; //當兩個指針裏的字符相等時,比較下一個字符
}
else
{
p1++; //不相等時p1往後一個字符
}
if(*p2 == '\0')
{
count++; //當p2到最後時代表p1中有和p2一樣的字符,讓count+1
p2 = str2; //讓p2再次等於str2
}
}
printf("%d\n",count); //輸出count的值
}
int main()
{
char str1[100] = {0};
char str2[100] = {0};
printf("please input the first string:\n");
scanf("%s",str1); //輸入字符串str1
printf("please input the second string:\n");
scanf("%s",str2); //輸入字符串str2
compare(str1,str2); //調用compare函數
return 0;
}