c语言字符串处理示例程序
说的具体点可以吗?
你的问题我没有办法回答的呀
------
回复此文章 |
变量主要是用数组,其它的等你问了再说吧。。
------
回复此文章 |
haohao说得对,提问题应该明晰、具体。
你的问题太笼统,让人难以作答。
我的回答如下:
由于c语言不支持字符串这种变量类型,所以,只好用字符数组来实现它的功能。
而c++就改正了这个缺点,支持字符串变量类型,java也支持。
c语言处理复杂的数据类型比较费劲,只好用结构体嵌套、结构体数组嵌套之类来实现。
而c++就可以用类来实现,不仅可以表示数据类型,还可以进行数据处理,即oop(面向对象的编程)是对c语言的重大改进,是程序员的福音。
------
回复此文章 |
能不能给一个使用字符串的事例
用c语言编的
在给一些详细的解释
先谢了:)
------
回复此文章 |
那给个用字符数组的例子:)
------
回复此文章 |
既然你喜欢看code,我就给你一个吧。
这是我们修改上网密码的程序的一部分。
// Rev. on 2003-10-28,packaged in a function.
#include "qqdefs.h"
int main(int argc, char *argv[])
{
char user[20];
char password[20];
char pwfile[100];
int rtn;
if(argc < 3) return usage();
// handling args, open files
strcpy(pwfile,argv[1]);
strcpy(user,argv[2]);
strcpy(password,argv[3]);
printf("%s %s %s\n",pwfile,user,password);
// main call
rtn = addqq(pwfile,user,password);
if (rtn == 1)
fprintf(stderr, "updating password for user %s\n",user);
else
fprintf(stderr, "Adding password for user %s\n",user);
} // end of main
------
回复此文章 |
// Rev. on 2003-10-28,packaged in a function.
#include <stdio.h>
#include <stdlib.h>
#define LF 10
#define CR 13
int getLine(char *s, int n, FILE *f)
{
register int i = 0;
while (1)
{
s<i> = (char) fgetc(f);
if (s<i> == CR)
{
s<i> = fgetc(f);
}
if ((s<i> == 0x4) || (s<i> == LF) || (i == (n-1)))
{
s<i> = '\0';
return (feof(f) ? 1 : 0);
}
++i;
}
}
void putLine(FILE *f, char *l)
{
int x;
for (x=0; l[x]; x++)
{
fputc(l[x], f);
}
fputc('\n', f);
}
void copyFile(FILE *target, FILE *source)
{
static char line[256];
while (fgets(line,sizeof(line), source) != NULL)
{
fputs(line,target);
}
}
int usage()
{
fprintf(stderr,"Usage:\n");
fprintf(stderr,"f5 pwfile user pass \n");
}
int addqq(char *pwfile,char *puser,char *ppass)
{
char qquser[20];
char pass[20];
char pfile[20];
FILE* fpw;
FILE* ftmp;
char line[256];
char scratch[256];
char record[256];
char emptyLine[]="\n";
int found = 0;
strcpy(qquser,puser);
strcpy(pass,ppass);
strcpy(pfile,pwfile);
// printf("%s %s %s",pfile,qquser,pass);
fpw = fopen(pfile, "r");
ftmp = fopen("socks5.tmp","w+");
while(!(getLine(line, sizeof(line), fpw)))
{
char *colon;
if((line[0] == '#') || (line[0] == '\0'))
{
putLine(ftmp,line);
continue; //skipping rem line, empty line.
}
strcpy(scratch,line);
/* See if this is our user. */
/* should added (unsigned char *) */
colon = (unsigned char *)strchr(scratch, ' ');
if(colon != NULL)
{
*colon = '\0';
}
if(strcmp(qquser, scratch) != 0)
{
putLine(ftmp,line);
continue; // not matched, skipping
}
found++;
break;
}
strcpy(record, qquser);
// Note not ':' but ":"
strcat(record, " ");
strcat(record, pass);
putLine(ftmp, record);
copyFile(ftmp, fpw);
fclose(fpw);
fclose(ftmp);
fpw = fopen(pfile, "w+");
ftmp = fopen("socks5.tmp", "r");
copyFile(fpw,ftmp);
return found;
}
------
回复此文章 |
程序其实很简单。但也许,你现在还看得费劲。
那你就努力看明白了,会有进步的。
------
回复此文章 |
上面的程序,颠来倒去可全是字符串啊。
字符串确实很重要。我的网管程序主要就是处理各种字符串(用户数据、计费数据)。
你的问题我没有办法回答的呀
------
回复此文章 |
变量主要是用数组,其它的等你问了再说吧。。
------
回复此文章 |
haohao说得对,提问题应该明晰、具体。
你的问题太笼统,让人难以作答。
我的回答如下:
由于c语言不支持字符串这种变量类型,所以,只好用字符数组来实现它的功能。
而c++就改正了这个缺点,支持字符串变量类型,java也支持。
c语言处理复杂的数据类型比较费劲,只好用结构体嵌套、结构体数组嵌套之类来实现。
而c++就可以用类来实现,不仅可以表示数据类型,还可以进行数据处理,即oop(面向对象的编程)是对c语言的重大改进,是程序员的福音。
------
回复此文章 |
能不能给一个使用字符串的事例
用c语言编的
在给一些详细的解释
先谢了:)
------
回复此文章 |
那给个用字符数组的例子:)
------
回复此文章 |
既然你喜欢看code,我就给你一个吧。
这是我们修改上网密码的程序的一部分。
// Rev. on 2003-10-28,packaged in a function.
#include "qqdefs.h"
int main(int argc, char *argv[])
{
char user[20];
char password[20];
char pwfile[100];
int rtn;
if(argc < 3) return usage();
// handling args, open files
strcpy(pwfile,argv[1]);
strcpy(user,argv[2]);
strcpy(password,argv[3]);
printf("%s %s %s\n",pwfile,user,password);
// main call
rtn = addqq(pwfile,user,password);
if (rtn == 1)
fprintf(stderr, "updating password for user %s\n",user);
else
fprintf(stderr, "Adding password for user %s\n",user);
} // end of main
------
回复此文章 |
// Rev. on 2003-10-28,packaged in a function.
#include <stdio.h>
#include <stdlib.h>
#define LF 10
#define CR 13
int getLine(char *s, int n, FILE *f)
{
register int i = 0;
while (1)
{
s<i> = (char) fgetc(f);
if (s<i> == CR)
{
s<i> = fgetc(f);
}
if ((s<i> == 0x4) || (s<i> == LF) || (i == (n-1)))
{
s<i> = '\0';
return (feof(f) ? 1 : 0);
}
++i;
}
}
void putLine(FILE *f, char *l)
{
int x;
for (x=0; l[x]; x++)
{
fputc(l[x], f);
}
fputc('\n', f);
}
void copyFile(FILE *target, FILE *source)
{
static char line[256];
while (fgets(line,sizeof(line), source) != NULL)
{
fputs(line,target);
}
}
int usage()
{
fprintf(stderr,"Usage:\n");
fprintf(stderr,"f5 pwfile user pass \n");
}
int addqq(char *pwfile,char *puser,char *ppass)
{
char qquser[20];
char pass[20];
char pfile[20];
FILE* fpw;
FILE* ftmp;
char line[256];
char scratch[256];
char record[256];
char emptyLine[]="\n";
int found = 0;
strcpy(qquser,puser);
strcpy(pass,ppass);
strcpy(pfile,pwfile);
// printf("%s %s %s",pfile,qquser,pass);
fpw = fopen(pfile, "r");
ftmp = fopen("socks5.tmp","w+");
while(!(getLine(line, sizeof(line), fpw)))
{
char *colon;
if((line[0] == '#') || (line[0] == '\0'))
{
putLine(ftmp,line);
continue; //skipping rem line, empty line.
}
strcpy(scratch,line);
/* See if this is our user. */
/* should added (unsigned char *) */
colon = (unsigned char *)strchr(scratch, ' ');
if(colon != NULL)
{
*colon = '\0';
}
if(strcmp(qquser, scratch) != 0)
{
putLine(ftmp,line);
continue; // not matched, skipping
}
found++;
break;
}
strcpy(record, qquser);
// Note not ':' but ":"
strcat(record, " ");
strcat(record, pass);
putLine(ftmp, record);
copyFile(ftmp, fpw);
fclose(fpw);
fclose(ftmp);
fpw = fopen(pfile, "w+");
ftmp = fopen("socks5.tmp", "r");
copyFile(fpw,ftmp);
return found;
}
------
回复此文章 |
程序其实很简单。但也许,你现在还看得费劲。
那你就努力看明白了,会有进步的。
------
回复此文章 |
上面的程序,颠来倒去可全是字符串啊。
字符串确实很重要。我的网管程序主要就是处理各种字符串(用户数据、计费数据)。
hofman
2005-11-19 22:06:38
评论:1
阅读:6131
引用:0
如何压缩空格
@2006-12-21 15:22:44 游客
比如把:
char a[30]={ abc de gf };
压缩成:char b[30]={abc de gf };
并且用到函数调用.请高手帮忙,谢谢!
char a[30]={ abc de gf };
压缩成:char b[30]={abc de gf };
并且用到函数调用.请高手帮忙,谢谢!
