geany这个工具在windows下使用居然有点略卡,编译过程,avast报警。。总体来说还比较好用,gnu c比ansi c好用不少。
小程序取自C程序设计语言里的题目2.4,实现了将字符串1中与字符串2中的相同字符剔除。
/*
* squeeze.c
*
* Copyright 2014 Administrator <fishland>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
#include <stdio.h>
#include <string.h>
#include <malloc.h>
int find(char*str, char c)
{
int strlength = strlen(str);
int i;
int ret = -1;
for(i = 0; i != strlength; i++)
{
if(str[i] == c)
{
ret = i;
}
}
return ret;
}
int del_char(char* str, int ofs)
{
int i, ret = 0;
int strlength = strlen(str);
if(ofs > strlength)
{
printf("invalid value..!\n");
ret = -1;
}
for(i = ofs; i < strlength; i++)
{
str[i] = str[i + 1];
}
return ret;
}
void squeeze(char* str1, char* str2)
{
if(str1 == NULL || str2 == NULL)
{
printf("error..!\n");
}
int strlength = strlen(str2);
char* col = (char*)malloc(strlength);
int i, j;
for(i = 0; i != strlength; i++)
{
while((j = find(str1, str2[i])) != -1)
del_char(str1, j);
}
free(col);
}
int main(int argc, char **argv)
{
char str1[] = "aaabbbcccdddfdasfjlajflajlajlfasflasflasjlas";
char str2[] = "afs";
printf("str1 = %s\n", str1);
printf("str2 = %s\n", str2);
printf("squeezing..!\n");
squeeze(str1, str2);
printf("str1 is squeezed to %s\n", str1);
return 0;
}