/*删除一个字符串中相同的字符*/
# include <stdio.h>
int main()
{
char s[27];
gets(s);
int i, j, k;
for (i=0; i<26; i++)
{
for (j=k=i+1; j<26; j++)
{
if (s[i] != s[j])
s[k++] = s[j];
}
s[k] = '\0';
}
puts(s);
return 0;
}
/*对一个数组不断的写入,然后返回值存放在一个指向指针的指针数组里面*/
# include <stdio.h>
# include <string.h>
# include <malloc.h>
int main()
{
char s[10];
int i;
char *ss[10];
for (i=0; i<10; i++)
{
gets(s);
ss[i] = strdup(s);
}
for (i=0; i<10; i++)
{
printf("%s\n", ss[i]);
}
return 0;
}
char *strdup(char *string)
{
char *new_string;
new_string = (char *)malloc(strlen(string) + 1);
if (new_string == NULL)
strcpy(new_string, string);
return new_string;
}
删除字符之间多余的空格
# include <stdio.h>
int main()
{
int c, lastc;
lastc = ' ';
while ((c = getchar()) != EOF)
{
if (c != ' ')
putchar(c);
else if (lastc != ' ')
putchar(c);
lastc = c;
}
return 0;
}
以每行一个单词输出
# include <stdio.h>
int main()
{
int c, state;
state = 0;
while ((c = getchar()) != EOF)
{
if (c == ' ' || c == '\n' || c == '\t')
{
if (state == 1)
{
putchar('\n');
state = 0;
}
}
else if (state == 0)
{
state = 1;
putchar(c);
}
else
{
putchar(c);
}
}
return 0;
}
# include <stdio.h>
# include <string.h>
# include <malloc.h>
# include <stdlib.h>
# define N 100
void print_tokens(char *s);
int main()
{
char s[N];
gets(s);
print_tokens(s);
return 0;
}
void print_tokens(char *s)
{
int count = 0;
char white[] = " \n\t\f\v\r";
char *token;
for (token=strtok(s, white); token != NULL; token=strtok(NULL, white))
{
count++;
puts(token);
}
printf("%d\n", count);
}
统计各个字符出现的次数
# include <stdio.h>
# include <ctype.h>
# define MAXCHAR 128
int main()
{
int c, i;
int cc[MAXCHAR] = {0};
while ((c = getchar()) != EOF)
{
if (c < MAXCHAR)
++cc[c]; //在数组中对应的位置上自增 最后输出该数即可
}
for (i=1; i<MAXCHAR; i++)
{
if (isprint(i))
printf("%5d-%c-%5d : ", i, i, cc[i]);
else
printf("%5d--%5d : ", i, cc[i]);
}
return 0;
}
字符数组转16进制
# include <stdio.h>
# define YES 1
# define NO 0
int htoi(char *s);
int main()
{
int n;
char s[100];
gets(s);
n = htoi(s);
printf("%d\n", n);
return 0;
}
int htoi(char *s)
{
int hexdigit, i , inhex, n;
i = 0;
if (s[i] == '0')
{
i++;
if (s[i] == 'x' || s[i] == 'X')
i++;
}
n = 0;
inhex = YES;
for ( ; inhex == YES; ++i)
{
if (s[i] >= '0' && s[i] <= '9')
hexdigit = s[i] - '0';
else if (s[i] >= 'a' && s[i] <= 'f')
hexdigit = s[i] - 'a' + 10;
else if (s[i] >= 'A' && s[i] <= 'F')
hexdigit = s[i] - 'A' + 10;
else
inhex = NO;
if (inhex == YES)
n = 16 * n + hexdigit;
}
return n;
}
从字符串s1中删除字符串s2中出现的字符
(不用标记的方法)
# include <stdio.h>
# define N 100
void squeeze(char *s1, char *s2);
int main()
{
char s1[N];
char s2[N];
gets(s1);
gets(s2);
squeeze(s1, s2);
puts(s1);
return 0;
}
void squeeze(char *s1, char *s2)
{
int i, j, k;
for (i=k=0; s1[i] != '\0'; i++)
{
for (j=0; s2[j] != '\0' && s2[j] != s1[i]; j++)
;
if (s2[j] == '\0')
s1[k++] = s1[i];
}
s1[k] = '\0';
}
在s1中匹配s2的字符串
# include <stdio.h>
# define MAXLINE 1000
int getline(char s[], int lim);
int strindex(char s[], char t[]);
char pattern[] = "ould";
int main()
{
char line[MAXLINE];
int found = 0;
while (getline(line, MAXLINE) > 0)
if (strindex(line, pattern) >= 0)
{
printf("%s\n", line);
found++;
}
return found;
}
int getline(char s[], int lim)
{
int c, i;
i = 0;
while (--lim > 0 && (c = getchar()) != EOF && c != '\n')
s[i++] = c;
if (c == '\n')
s[i++] = c;
s[i] = '\0';
return i;
int strindex(char s[], char t[])
{
int i, j, k;
for (i=0; s[i] != '\0'; i++)
{
for (j=i, k=0; t[k] != '\0' && s[j] == t[k]; j++, k++)
;
if (k > 0 && t[k] == '\0')
return i;
}
return -1;
}
科学计数法转十进制
# include <stdio.h>
# include <ctype.h>
double atof(char s[]);
int main()
{
double value;
char s[10];
printf("请输入你需要转换的科学表示法:\n");
gets(s);
value = atof(s);
printf("%lf\n", value);
return 0;
}
double atof(char s[])
{
double val, power;
int exp, i, sign;
for (i=0; isspace(s[i]); i++)
;
sign = (s[i] == '-') ? -1 : 1;
if (s[i] == '+' || s[i] == '-')
i++;
for (val = 0.0; isdigit(s[i]); i++)
val = 10.0 * val + (s[i] - '0');
if (s[i] == '.')
i++;
for (power=1.0; isdigit(s[i]); i++)
{
val = 10.0 * val + (s[i] - '0');
power *= 10.0;
}
val = sign * val / power;
if (s[i] == 'e' || s[i] == 'E')
{
sign = (s[++i] == '-') ? -1 : 1;
if (s[i] == '+' || s[i] == '-')
i++;
for (exp=0; isdigit(s[i]); i++)
exp = 10 * exp + (s[i] - '0');
if (sign == 1)
while (exp-- > 0)
val *= 10;
else
while (exp-- > 0)
val /= 10;
}
return val;
}
递归将数字转化成字符串
# include <stdio.h>
void fun(int a);
int main()
{
int a;
scanf("%d", &a);
fun(a);
return 0;
}
void fun(int a)
{
if (a < 0)
{
putchar('-');
a = -a;
}
if (a / 10)
fun(a / 10);
putchar(a % 10 + '0');
}
检测s1的后边是不是字符串s2
# include <stdio.h>
# define N 100
bool strend(char *s1, char *s2);
int main()
{
char s1[N];
char s2[N];
gets(s1);
gets(s2);
if ( strend(s1, s2) )
printf("YES\n");
else
printf("NO\n");
return 0;
}
bool strend(char *s1, char *s2)
{
char *ps1 = s1;
char *ps2 = s2;
while (*s1)
s1++;
while (*s2)
s2++;
for ( ; *s1 == *s2; s1--, s2--)
if (s1 == ps1 || s2 == ps2)
break;
if (*s1 == *s2 && s2 == ps2 && *s1 != '\0')
return true;
else
return false;
}