第11 章字符串 和字符串 函數(shù)

使用 字符串 用戶 交互?

#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");

}




用戶自定義的輸出函數(shù)?

#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';

}

}




解決錯誤,所短字符串的函數(shù)?

指針是一個常量,, 數(shù)組是一個變量

#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()函數(shù)

#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()函數(shù)的使用


#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;

}






帶有參數(shù)的 main函數(shù)?

#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");

}


最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市错邦,隨后出現(xiàn)的幾起案子轰胁,更是在濱河造成了極大的恐慌哀墓,老刑警劉巖柴灯,帶你破解...
    沈念sama閱讀 211,639評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件悍汛,死亡現(xiàn)場離奇詭異滑负,居然都是意外死亡在张,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,277評論 3 385
  • 文/潘曉璐 我一進店門矮慕,熙熙樓的掌柜王于貴愁眉苦臉地迎上來帮匾,“玉大人,你說我怎么就攤上這事痴鳄∥列保” “怎么了?”我有些...
    開封第一講書人閱讀 157,221評論 0 348
  • 文/不壞的土叔 我叫張陵痪寻,是天一觀的道長螺句。 經(jīng)常有香客問我,道長橡类,這世上最難降的妖魔是什么蛇尚? 我笑而不...
    開封第一講書人閱讀 56,474評論 1 283
  • 正文 為了忘掉前任,我火速辦了婚禮顾画,結果婚禮上取劫,老公的妹妹穿的比我還像新娘。我一直安慰自己研侣,他們只是感情好谱邪,可當我...
    茶點故事閱讀 65,570評論 6 386
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著庶诡,像睡著了一般惦银。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,816評論 1 290
  • 那天扯俱,我揣著相機與錄音书蚪,去河邊找鬼。 笑死蘸吓,一個胖子當著我的面吹牛善炫,可吹牛的內容都是我干的。 我是一名探鬼主播库继,決...
    沈念sama閱讀 38,957評論 3 408
  • 文/蒼蘭香墨 我猛地睜開眼箩艺,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了宪萄?” 一聲冷哼從身側響起艺谆,我...
    開封第一講書人閱讀 37,718評論 0 266
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎拜英,沒想到半個月后静汤,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,176評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡居凶,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 36,511評論 2 327
  • 正文 我和宋清朗相戀三年虫给,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片侠碧。...
    茶點故事閱讀 38,646評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡抹估,死狀恐怖,靈堂內的尸體忽然破棺而出弄兜,到底是詐尸還是另有隱情药蜻,我是刑警寧澤,帶...
    沈念sama閱讀 34,322評論 4 330
  • 正文 年R本政府宣布替饿,位于F島的核電站语泽,受9級特大地震影響,放射性物質發(fā)生泄漏视卢。R本人自食惡果不足惜踱卵,卻給世界環(huán)境...
    茶點故事閱讀 39,934評論 3 313
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望据过。 院中可真熱鬧颊埃,春花似錦、人聲如沸蝶俱。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,755評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽榨呆。三九已至,卻和暖如春庸队,著一層夾襖步出監(jiān)牢的瞬間积蜻,已是汗流浹背闯割。 一陣腳步聲響...
    開封第一講書人閱讀 31,987評論 1 266
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留竿拆,地道東北人宙拉。 一個月前我還...
    沈念sama閱讀 46,358評論 2 360
  • 正文 我出身青樓,卻偏偏與公主長得像丙笋,于是被迫代替她去往敵國和親谢澈。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 43,514評論 2 348

推薦閱讀更多精彩內容