使用 字符串 用戶 交互?
#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 ",©[0]);
printf("mesg = %s? & mesg = %p? value = %p \n ",mesg, &mesg,&mesg[0]);
printf("copy = %s? & mesg = %p? value = %p \n ",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");
}