完成几道字符串题目

This commit is contained in:
nonstriater
2015-12-01 16:26:04 +08:00
parent a2d615a9fc
commit 47972d3f05
6 changed files with 321 additions and 109 deletions
@@ -0,0 +1,28 @@
#include "stdio.h"
char char_first_appear_once(const unsigned char *source){
int hash[256]={0};
char *tmp = source;
if (tmp == NULL) return '\0';
while(*tmp != '\0'){
hash[*tmp]++;
tmp++;
}
tmp = source;
while(*tmp != '\0'){
if (hash[*tmp] == 1) return *tmp;
tmp++;
}
return '\0';
}
int main(int argc, char const *argv[])
{
char *test = "77ah-ba-ccdeff";
char c = char_first_appear_once(test);
printf("c=%c\n", c);/* c = 'h'*/
return 0;
}
@@ -0,0 +1,32 @@
#include "stdio.h"
char *replace_blank(char *source){
int count = 0;
char *tail = source;
if (source == NULL) return NULL;
while(*tail != '\0'){
if (*tail == ' ') count++;
tail++;
}
while(count){
if(*tail != ' '){
*(tail+2*count) = *tail;
}else{
*(tail+2*count) = '0';
*(tail+2*count-1) = '2';
*(tail+2*count-2) = '%';
count--;
}
tail--;
}
return source;
}
int main(int argc, char const *argv[])
{
char str[100]="we are happy";
printf("ret=%s\n",replace_blank(str));
return 0;
}