1.设计一个函数,可以从输入读取n个字符(包括空格、制表、和换行符),把结果保存在一个数组中,这个数组的地址通过参数传递。
#include <stdio.h>
#include <string.h>
#define SIZE 100
void input(char *string);
int main(void)
{
int i;
char data[SIZE];
input(data);
puts(data);
puts("END!");
for(i=0;i<SIZE;i++)
printf("%d\n",data[i]);
return 0;
}
void input(char *string)
{
int i=0;
puts("Please enter characters.");
while(i<SIZE)
{
string[i]=getchar();
if(string[i]==EOF)
{
string[i]='\0';
break;
}
i++;
}
}
2.修改测试练习1中的函数,使得可以在n个字符后,或者一个空格、制表符、换行符后停止读取输入,由上述情况最先满足的那个终止读取(不能用scanf()函数)。
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define SIZE 100
void input(char *string);
int main(void)
{
int i;
char data[SIZE];
input(data);
puts(data);
puts("END!");
for(i=0;i<SIZE;i++)
printf("%d\n",data[i]);
return 0;
}
void input(char *string)
{
int i=0;
puts("Please enter characters.");
while(i<SIZE)
{
string[i]=getchar();
if(isspace(string[i]))
{
string[i]='\0';
break;
}
i++;
}
}
3.设计并测试一个函数,其功能是读取输入行里面的第一个单词到数组,并丢掉该行中的其他字符。一个单词的定义是一串字符,其中不含空格,制表符和换行符。
#include <stdio.h>
#include <ctype.h>
const int SIZE=100;
void getstring(char *str,int SIZE);
int main(void)
{
int i;
char string[SIZE];
getstring(string,SIZE);
puts(string);
for(i=0;i<SIZE;i++)
printf("%d ",string[i]);
return 0;
}
void getstring(char *str,int SIZE)
{
int i=0;
char ch;
printf("Enter your character.\n");
while(((ch=getchar())!=EOF) && i<SIZE-1)
{
if(isspace(ch)==false)
{
str[i]=ch;
i++;
}
}
str[i]='\0';
}
4.设计并测试一个函数,其功能是搜索由函数的第一个参数指定字符串,在其中查找由函数的第二个参数指定的字符的第一次出现的位置。如果找到,返回指向这个字符的指针;如果没找到,返回空字符。在一个使用循环语句中为这个函数提供输入的完整程序中进行测试。
#include <stdio.h>
#include <ctype.h>
const int SIZE=100;
char * search(char *string,char ch);
int main(void)
{
char string[SIZE]="wyw";
char ch='w';
printf("Please enter a string,I can find the address of the first character 'w'!\nEOF to quit:\n");
while(gets(string))
{
search(string,ch);
printf("Please enter another character.\nEOF to quit:\n");
}
return 0;
}
char * search(char *string,char ch)
{
char *rt=NULL;
while(*string)
{
if(*string==ch)
{
rt=string;
break;
}
string++;
}
if(rt)
{
printf("%x\n",rt);
}
else
{
printf("Not found!\n");
}
return rt;
}
5.编写一个函数is_within()。它接受两个参数,一个是字符,另一个是字符指针。其功能是如果字符在字符串中,就返回一个非0(真)值;如果字符不在字符串中,就返回0(假)。在一个使用循环语句为这个函数提供输入的完整程序中测试。
#include <stdio.h>
#include <ctype.h>
const int SIZE=100;
int search(char *string,char ch);
int main(void)
{
char string[SIZE]="wyw";
char ch='w';
printf("Please enter a string.\nEOF to quit:\n");
while(gets(string))
{
search(string,ch);
printf("Please enter another string.\nEOF to quit:\n");
}
return 0;
}
int search(char *string,char ch)
{
int rt=0;
while(*string)
{
if(*string==ch)
{
rt=1;
break;
}
string++;
}
if(rt)
{
printf("I found it\n");
}
else
{
printf("Not found!\n");
}
return rt;
}
6.strncpy(s1,s2,n)函数从s2复制n个字符给s1,并在必要时截断s2或为其填充额外的空字符。如果s2的长度等于或者大于n,目标字符串就没有标志结束的控制符。函数返回s1.自己编写这个函数,并在一个使用循环语句中为这个函数提供输入的完整程序中测试。
#include <stdio.h>
#include <string.h>
const int SIZE=10;
char *copy(char *str1,char *str2,int n);
int main(void)
{
char str1[SIZE]="abc";
char str2[SIZE];
int i;
while(gets(str2))
{
copy(str1,str2,10);
for(i=0;i<SIZE;i++)
{
printf("%d ",str1[i]);
}
}
return 0;
}
char *copy(char *str1,char *str2,int n)
{
int i=0;
int j=0;
while(str1[i]!=0)
i++; //i就是str1的空字符下标
printf("str1[i]=%x\n",str1[i]);
for(j=0;j<n;i++,j++)
{
if(i<SIZE-1) //SIZE-2是最后一个能放str2复制过来的字符的空间
str1[i]=str2[j];
else
str1[i]='\0';
}
return str1;
}
7.编写一个函数string_in(),它接受两个字符串指针做参数。如果第二个字符串被包含在第一个字符串中,函数就返回被包含的字符串开始的地址。例如string_in("hats","at")返回hats中a的地址,否则,函数返回空指针。在一个使用循环的语句中为这个函数提供输入的完整程序中测试。
#include <stdio.h>
#include <string.h>
const int SIZE=100;
char *find(char *str1,char *str2);
int main(void)
{
char str1[SIZE]="KYJ is a beautifull girl";
char str2[SIZE];
gets(str2);
puts(str2);
if(find(str1,str2))
{
printf("Yes,I found the str2 is in the str1.the address is %x\n",find(str1,str2));
}
else
{
printf("I can't find it\n");
}
}
char *find(char *str1,char *str2)
{
char *rt=NULL;
int str2_size=strlen(str2);
int i=0;
for(i=0;i<=SIZE-1-str2_size;i++)
{
if(strncmp(str1+i,str2,str2_size)==0)
{
rt=str1+i;
}
}
return rt;
}
8.编写一个函数,其功能是使输入字符串反序。在一个使用循环语句为这个函数提供输入的完整程序中测试。#include <stdio.h>
#include <string.h>
#define SIZE 100
void antiton(char *str);
int main(void)
{
char string[SIZE];
while(gets(string))
{
puts(string);
antiton(string);
puts(string);
}
return 0;
}
void antiton(char *str)
{
int i;
int len=strlen(str);
printf("len=%d\n",len);
char temp;
for(i=0;i<len/2;i++)
{
temp=str[i];
str[i]=str[len-i-1];
str[len-i-1]=temp;
}
}
9.编写一个函数。其参数为一个字符串,函数删除字符串中的空格。在一个可以循环读取中的程序进行测试,直到用户输入空行。对于任何输入字符串,函数都应该可以使用并可以显示结果。
#include <stdio.h>
#include <string.h>
#define SIZE 100
void del_space(char *str);
int main(void)
{
char string[SIZE];
while(gets(string))
{
puts(string);
del_space(string);
}
return 0;
}
void del_space(char *str)
{
char temp[SIZE];
int i,j;
for(i=0,j=0;str[i];i++)
{
if(str[i]!=' ')
{
temp[j]=str[i];
j++;
}
}
temp[j]='\0';
for(i=0;i<=strlen(temp);i++)
{
str[i]=temp[i];
printf("%d ",str[i]);
}
}
10.编写一个函数,读取输入,直到读入10个字符串或者遇到EOF,由二者中最先被满足的那个终止读取过程。这个程序可以为用户提供一个有5个选项的菜单:输出初始字符串列表、按ASCII顺序输出字符串、按长度递增书序输出字符串、按字符串中第一个单词的长度输出字符串和退出。才当可以循环、直到用户输入退出请求。当然,程序要能完成菜单中的各项功能。<pre name="code" class="cpp">#include <stdio.h>
#include <string.h>
#define SIZE 100
#define LIM 10
void show_menu(void);
void print_str(const char str[][SIZE],int lim);
void sort_ascii(char str[][SIZE],int lim);
void sort_min(char *string);
void sort_strlen( char str[][SIZE],int lim);
void sort_1thwordlen(char str[][SIZE],int lim);
int main(void)
{
char string[LIM][SIZE];
int choice;
int i=0;
puts("Please enter your string");
puts("It will quit while entring EOF or over 10");
show_menu();
while(i<LIM && (gets(string[i]))!=NULL)
i++;
printf("please select what you want to do.\n");
scanf("%d",&choice);
while(choice!=5)
{
switch(choice)
{
case 1:
print_str(string,LIM);break;
case 2:
sort_ascii(string,LIM);break;
case 3:
sort_strlen(string,LIM);break;
case 4:
sort_1thwordlen(string,LIM);break;
case 5:
printf("You will quit pragram.\n");break;
default:
printf("Please Enter number of 1-5.\n");continue;
}
printf("please select what you want to do.\n");
scanf("%d",&choice);
if(choice==5)
break;
}
printf("End");
}
void show_menu(void)
{
printf("\n***************************************************\n");
printf(" This is a function menu.\n");
printf("1: print your array of string.\n");
printf("2: sort the string in order of ASCII.\n");
printf("3: print string in order of length.\n");
printf("4: sort the string in order of first word's length.\n");
printf("5: quit.\n");
printf("***************************************************\n");
}
void print_str(const char str[][SIZE],int lim)
{
int i=0;
printf("\n*******************************************************\n");
while(i<lim && *(str[i])!=NULL)
{
puts(str[i]);
i++;
}
printf("*******************************************************\n");
}
/*void select(int *choice)
{
puts("Please enter your choice.");
scanf("%d",choice);
switch(*choice)
{
case 1:
print_str()
}
}*/
void sort_ascii(char str[][SIZE],int lim)
{
int i;
for(i=0;i<lim && *(str[i])!=NULL;i++)
{
sort_min(str[i]);
}
print_str(str,lim);
}
void sort_min(char *string)
{
int i,j;
char temp;
for(i=0;i<strlen(string);i++)
{
for(j=i+1;j<strlen(string);j++)
{
if(string[j]<string[i])
{
temp=string[i];
string[i]=string[j];
string[j]=temp;
}
}
}
}
void sort_strlen( char str[][SIZE],int lim)
{
char *pst[LIM];
int i,j,x;
char * temp=NULL;
for(i=0;i<lim && *(str[i])!=NULL;i++)
{
pst[i]=str[i];
}
for(j=0;j<i;j++)
{
for(x=j+1;x<i;x++)
{
if( strlen(pst[x]) < strlen(pst[j]) )
{
temp=pst[j];
pst[j]=pst[x];
pst[x]=temp;
}
}
}
for(j=0;j<i;j++)
{
puts(pst[j]);
}
}
void sort_1thwordlen(char str[][SIZE],int lim)
{
char *pst[LIM];
int i,j,x;
char * temp=NULL;
for(i=0;i<lim && *(str[i])!=NULL;i++)
{
pst[i]=str[i];
}
for(j=0;j<i;j++)
{
for(x=j+1;x<i;x++)
{
if( strlen(pst[x]) < strlen(pst[j]) )
{
temp=pst[j];
pst[j]=pst[x];
pst[x]=temp;
}
}
}
for(j=0;j<i;j++)
{
puts(pst[j]);
}
}
11.编写一个程序。功能是读取输入,直到遇到EOF,并报告单词书、大写字母数、小写字母数、标点符号数和数字字符数。使用ctype.h系列的函数。
#include <stdio.h>
#include <ctype.h>
int main(void)
{
int word=0,upper=0,lower=0,punct=0,digit=0,begin=0;
char ch;
while( ( ch = getchar() ) != EOF )
{
if( isalpha(ch) )
{
if (begin == 0)
{
word++;
begin = 1;
}
}
else
begin = 0;
if( isupper(ch) ) upper++;
if( islower(ch) ) lower++;
if( ispunct(ch) ) punct++;
if( isdigit(ch) ) digit++;
}
printf("word:%d\nupper:%d\nlower:%d\npunct:%d\ndigit:%d\n",word,upper,lower,punct,digit);
return 0;
}
12.编写一个程序,按照相反的单词顺序显示命令行参数。即,如果命令行参数是see you later,则程序应该输出later you see#include <stdio.h>
#include <string.h>
int main(int argc,char *argv[])
{
int count=argc;
for(count=argc;count>=1;count--)
{
puts(argv[count]);
}
}
13.编写一个计算乘幂的基于命令行的程序,第一个命令行参数为double类型数据,作位幂的底数。第二个参数为整数,作为指数。