第11 章字符串 和字符串 函数

使用 字符串 用户 交互 

#include <stdio.h>

#include < stdlib.h>

#define MSG "you must have many talents tell me some "

#define LIM 5

#define LINELEN 81

void main()

{

char name [LINELEN];

char talents [LINELEN];

int i  = 0;

const char m1[40] = "Limit yourself to one line's worth";

const char *m2 = " If your can't think of anything fake it ";

const char *m3 = "\n Enough about me - what's your name?";

const char *mytal[LIM] = {

"Adding numbers swiftly ",

"Multiplying accurately ",

"stashing data ",

"Following instructions to the letter ",

"Understanding the C language "

};

printf("Let me tell you some of them .\n");

puts ("what were they ? ah yes heres a partial list ");

for ( i ;  i  < LIM;  i ++)

{

puts(*(mytal+i));

}

puts("\n");

puts(m3);

gets(name);

printf("well %s ,%s \n",name,MSG);

printf("%s \n %s \n ",m1, m2);

gets(talents);

puts("Let's see if i 've got that list");

puts(talents);

printf("think %s",name);

system("pause");

}




把字符串 看作 指针 

#include <stdio.h>

#include < stdlib.h>

#define MSG "you must have many talents tell me some "

#define LIM 5

#define LINELEN 81

void main()

printf("%s , %p %c","we","are",*"space farers");

system("pause");

}


指针 和字符串 

#include <stdio.h>

#include < stdlib.h>

#define MSG "you must have many talents tell me some "

#define LIM 5

#define LINELEN 81

void main()

char * mesg = "don't be a fool11!";

char * copy;

copy = mesg;

printf("%s \n ",&copy[0]);

printf("mesg = %s  & mesg = %p  value = %p \n ",mesg, &mesg,&mesg[0]);

printf("copy = %s  & mesg = %p  value = %p \n ",copy, &copy,copy);

system("pause");

}

读取一个名字



#include <stdio.h>

#include < stdlib.h>

#define MSG "you must have many talents tell me some "

#define LIM 5

#define LINELEN 81

void main()

char name[LINELEN] ;

gets(name);

printf("%s",&name[0]);

system("pause");

}






读取一个名字 

#include <stdio.h>

#include < stdlib.h>

#define MSG "you must have many talents tell me some "

#define LIM 5

#define LINELEN 81

void main()

char name[LINELEN] ;

char *p;

p = gets(name);

printf("%s      , %s",&name[0] ,& p[0]);

system("pause");

}




使用fgets 读取一个名字 

#include <stdio.h>

#include < stdlib.h>

#define MSG "you must have many talents tell me some "

#define LIM 5

#define LINELEN 81

void main()

char name[LINELEN] ;

char *p;

p = fgets(name,LINELEN,stdin);

printf("%s      , %s",&name[0] ,& p[0]);

system("pause");

}

使用 


使用scanf 

#include <stdio.h>

#include < stdlib.h>

#define MSG "you must have many talents tell me some "

#define NAME 5

#define LINELEN 11

void main()

char name[NAME] , name2[LINELEN];

int a ;

a = scanf("%5s %10s",name,name2);

printf("%s,%s",&name[0] ,& name2[0]);

system("pause");

}




使用 puts()

#include <stdio.h>

#include < stdlib.h>

#define MSG "you must have many talents tell me some "

#define NAME 5

#define LINELEN 11

void main()

char str1[80] = "An array was initialized to me ";

const char * str2 = "a, pointer was initialized to me";

puts("I' m an argument to puts");

puts(MSG);

puts( &*(str1+0));

puts(  str1 );

puts(str2);

puts( &str1[5]);

puts(&(*(str1 +5)));

puts( str2+4);

puts( &str2[4]);

system("pause");

}




用户自定义的输出函数 

#include <stdio.h>

#include < stdlib.h>

#define MSG "you must have many talents tell me some "

#define NAME 5

#define LINELEN 11

void put1(const char *);

int put2 ( const char *);

void main()

put1("if I'd as much money");

put1("as I could spend \n");

printf("Icount %d characters.\n" , put2("I never would cry olld chairs to mend "));

system("pause");

}

void put1(const char * string )

{

while (*string )

{

putchar(*string++);

}

}

int put2 ( const char * string)

{

int count = 0;

while (*string )

{

putchar(*string++);

count++;

}

return count;

}




代码错误.

#include <stdio.h>

#include <string.h>

#include < stdlib.h>

void put2 ( char * , unsigned int);

void main()

char *mesg =" Hold on to your hats , hackers";

puts(mesg);

put2(&*(mesg+0),7);

puts(mesg);

system("pause");

}

void put2 (  char * string , unsigned int size)

{

if (strlen(string) > size )

{

string[size] = '\0';

}

}




解决错误,所短字符串的函数 

指针是一个常量,, 数组是一个变量

#include <stdio.h>

#include <string.h>

#include < stdlib.h>

void put2 ( char * , unsigned int);

void main()

char mesg[] =" Hold on to your hats , hackers";

puts(mesg);

put2(&*(mesg+0),7);

puts(mesg);

puts(mesg+8);

system("pause");

}

void put2 (  char * string , unsigned int size)

{

if (strlen(string) > size )

{

string[size] = '\0';

}

}




连接两个字符串 

#include <stdio.h>

#include <string.h>

#include < stdlib.h>

#define SIZE 80

void main()

char flower [SIZE];

char addon[] = "s smell like old shoes ";

gets(flower);

strcat(flower,addon);

puts(flower);

puts(addon);

system("pause");

连接两个字符串 并且 ,检查第一个字符串的大小

#include <stdio.h>

#include <string.h>

#include < stdlib.h>

#define SIZE 30

#define BUGSIZE 13

void main()

char flower [SIZE];

char addon[] = "s smell like old shoes ";

char bug[BUGSIZE];

int available;

puts("what is your favorite flower ? ");

gets(flower);

if ((strlen(addon) + strlen(flower)+1) <= SIZE)

{

strcat(flower, addon);

}

puts ("what is your favorite bug?");

gets(bug);

available = BUGSIZE - strlen(bug)-1;

strncat(bug,addon,available);

puts(bug);

system("pause");



STRCMP 满足要求的程序

#include <stdio.h>

#include <string.h>

#include < stdlib.h>

#define MAX 40

#define ANSWER "grant"

void main()

char try1[MAX];

puts("who is buried in grant's tomb?");

gets(try1);

while(strcmp(try1,ANSWER)  )

{

puts("No that's wrong try again ");

gets(try1);

}

puts("that 's right");

system("pause");





strcmp 返回值


#include <stdio.h>

#include <string.h>

#include < stdlib.h>

#define MAX 40

#define ANSWER "grant"

void main()

printf("strcmp (\" A \" , \" A \" )is ");

printf("%d",strcmp ("A","A"));

printf("strcmp (\" A \" , \"b \" )is \n");

printf("%d",strcmp ("A","b"));

printf("%d",strcmp ("hello","hel"));

system("pause");



使用strncmp()函数

#include <stdio.h>

#include <string.h>

#include < stdlib.h>

#define LISTSIZE 5

#define MAX 40

void main()

char * list[ LISTSIZE] = {

"astronomy",

"astounding",

"astrophysics ",

"ostracize",

"asterism"

};

int count = 0;

int i ;

for (  i = 0; i < LISTSIZE; i++)

{

if (strncmp(list[i],"astro",5) == 0)

{

printf("Found : %s\n",list[i]);

count++;

}

}

printf("the list contained %d words beginning with astro ",count);

system("pause");





strcpy()函数的使用


#include <stdio.h>

#include <string.h>

#include < stdlib.h>

#define WORDS "beast"

#define SIZE 40

#define LIM 5

void main()

char qword [LIM][SIZE];

char temp[SIZE];

int i = 0;

printf("enter %d words beginning with q \n",LIM);

while (i < LIM && gets(temp))

{

if (temp[0] != 'q')

{

printf("%s doesn't begin with q !",temp);

}else

{

strcpy(qword[i],temp);

i++;

}

}

puts("here are the words accepted : ");

for (  i = 0; i < LIM; i++)

{

puts(qword[i]);

}

system("pause");




strncpy()

#include <stdio.h>

#include <string.h>

#include < stdlib.h>

//#define WORDS "beast"

#define SIZE 40

#define LIM 5

#define TARGSIZE 7

void main()

char qword [LIM][SIZE];

char temp[SIZE];

int i = 0;

printf("enter %d words beginning with q \n",LIM);

while (i < LIM && gets(temp))

{

if (temp[0] != 'q')

{

printf("%s doesn't begin with q !",temp);

}else

{

strncpy(qword[i],temp ,TARGSIZE -1 );

qword[i][TARGSIZE - 1] = '\0';

i++;

}

}

puts("here are the words accepted : ");

for (  i = 0; i < LIM; i++)

{

puts(qword[i]);

}

system("pause");







strcpy高级属性 

#include <stdio.h>

#include <string.h>

#include < stdlib.h>

#define WORDS "beast"

#define SIZE 40

//#define LIM 5

void main()

char *orig = WORDS;

char copy[SIZE] = "Be the best that you can be ";

char *ps;

puts(orig);

  puts(copy);

ps = strcpy(copy +7 ,orig);

puts(copy);

puts(ps);

system("pause");


格式化一个字符串 

#include <stdio.h>

#include <string.h>

#include < stdlib.h>

#define WORDS "beast"

#define MAX 20

//#define LIM 5

void main()

{   

char first[MAX] ;

char list [MAX];

char formal[2 * MAX+10];

double prize;

puts("enter your first name");

gets(first);

gets(list);

puts("enter your prize noney");

scanf("%lf",&prize);

sprintf(formal,"%s , %-19s $%6.2f\n" ,list ,first,prize);

puts( formal);

system("pause");

读进一些 字符串并 进行排序 



#include <stdio.h>

#include <string.h>

#include < stdlib.h>

#define SIZE 81

#define LIM 20

#define HALT " "

void  stsrt (char *string[],int );

void main()

{   

char input [LIM][SIZE] ;

char *ptstr[LIM];

int ct = 0;

int k;

printf("Input up to %d lines and Iwill sort them \n",LIM);

printf("to stop press the enter key at a limne start \n");

while (ct < LIM && gets (*(input +ct) ) != NULL && *(*(input+ct)+0) != '0')

{

ptstr[ct] = input[ct];

ct++;

}

stsrt(ptstr,ct);

puts("\n here's the sorted list \n");

for (  k = 0; k < ct; k++)

{

puts(*(ptstr+k));

}

system("pause");

void  stsrt (char *string[],int num)

{

char *temp;

int top, seek;

for (top = 0; top< num-1; top++)

{

for (  seek = top; seek < num ; seek++)

{

if (strcmp (*(string + top), string [seek]) > 0)

{

temp = string [top];

string [top] = string[seek];

string[seek] = temp;

}

}

}

}



修改字符串, 大小写转换 


#include <stdio.h>

#include <string.h>

#include < stdlib.h>

#include <ctype.h>

#define LIMIT 81

void  ToUpper (char *  );

int PunctCount (const char*);

void main()

{   

char line [LIMIT];

puts("Please enter a line");

gets(line);

ToUpper(line);

puts(line);

printf("that line has %d punctuation charachters \n",PunctCount(line));

system("pause");

}

void  ToUpper (char * str )

{

while (*str  )

{

*str = toupper (*str);

str++;

}

}

int PunctCount (const char* str)

{

int ct = 0;

while (*str  )

{

if (ispunct (*str))

{

ct++;

}

str++;

}

return ct;

}






带有参数的 main函数 

#include <stdio.h>

#include <string.h>

#include < stdlib.h>

#include <ctype.h>

#define LIMIT 81

void main(int argc ,char *argv[])

{   

int count ;

printf("the command line has %d arguments \n",argc -1);

for (  count = 1; count < argc; count++)

{

printf("%d %s \n ",count, argv[count]);

}

system("pause");

  }






#include <stdio.h>

#include < stdlib.h>

void main()

{

char number[30];

char *end;

long value;

puts("Enter a number (empty line to quit )");

while(gets ( number) && number [0] != '\0')

{

value = strtol (number , & end , 10);

printf("value %d , stopped at %s (%d)\n",value,end,*end) ;

value = strtol (number , & end , 16 );

printf("value : %ld, stopped at %s(%d)\n",value,end,*end);

puts("next number ");

}

puts ("bey!\n");

system("pause");

}


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,504评论 6 496
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,434评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 160,089评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,378评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,472评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,506评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,519评论 3 413
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,292评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,738评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,022评论 2 329
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,194评论 1 342
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,873评论 5 338
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,536评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,162评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,413评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,075评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,080评论 2 352

推荐阅读更多精彩内容