编写一个交换变量值的函数,利用该函数交换数组a和数组b中对应元素值
设计思想:用指针变量作为函数参数实现两数交换。定义两个指针变量作为形参,在调用时分别指向两个数组的相应位置
#include <stdio.h>
#define SIZE 10
void swap(int *x,int *y);
int main()
{
int a[SIZE],b[SIZE],i,n;
printf("Input array length n<=%d:",SIZE);
scanf("%d",&n);
printf("Input array a:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Input array b:");
for(i=0;i<n;i++)
scanf("%d",&b[i]);
for(i=0;i<n;i++)
swap(&a[i],&b[i]);
printf("After swap:\n");
printf("Array a:");
for(i=0;i<n;i++)
printf("%d",a[i]);
printf("\n");
printf("Array b:");
for(i=0;i<n;i++)
printf("%d",b[i]);
printf("\n");
}
void swap(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
不用strcat函数,编写实现字符串连接strcat的功能,将字符串t连接到字符串s的尾部
设计思想:用i和j分别作为字符数组dstStr和字符数组srcStr的下标,先将i和j同时初始化为0,然后移动i使其位于字符dstStr的尾部,即字符串结束标识符,再将字符数组中的字符依次复制到字符数组dstStr中。
#include <stdio.h>
void Mystrcat(char *dstStr, char *srcStr);
int main()
{
char s[80],t[80];
printf("please input the first string: ");
gets(s);
printf("please input the second string: ");
gets(t);
Mystrcat(s,t);
printf("The concat is: ");
puts(s);
}
void Mystrcat(char *dstStr,char *srcStr)
{
while(*dstStr!='\0')
dstStr++;
while(*srcStr !='\0')
{
*dstStr=*srcStr;
dstStr++;
srcStr++;
}
*dstStr='\0';
}
编程判断输入的一串字符是否为“回文”。
设计思想:对称的字符串
设置两个指针pStart和pEnd,让pStart指向字符串首部,让pEnd指向字符串尾部
利用循环,从字符串两边对指针所指字符进行比较,当对应的两字符相等且两指针未超越对方时,使指针pStrat向后移动一个字符位置(即加1),使指针pEnd向前移动一个字符位置(即减1),一旦发现对应的两个字符不等或两指针已相互超越(不可能是回文),则立即停止循环
根据退出循环时两指针的位置,判断字符串是否为回文
#include<stdio.h>
#include<string.h>
int main()
{
char str[80],*pStart,*pEnd;
int len;
printf("Input String: ");
gets(str);
len=strlen(str);
pStart=str;
pEnd=str+len-1;
while(*pStart==*pEnd && pStart<=pEnd)
{
pStart++;
pEnd--;
}
if(pStart<pEnd)
printf("No!\n");
else
printf("Yes!\n");
}
4、编写一取某字符子串的函数char*substr(char *s,int startloc,int len),其中s为字符串,startloc为起始位置(0表示第一个字符的位置),len为子串的长度。要求返回值为求得的子串。
设计思想:
因为该函数返回指向子串的字符型指针,所以在函数中可以定义一个静态字符型数组sstr,用于存放所取得的子串。取子串的方法可以从字符串s的第startloc个字符开始复制到静态数组sstr中,直到已复制了Len个字符或遇到了字符串结尾标志。然后将sstr返回即可。
#include<stdio.h>
#include<string.h>
#include<malloc.h>
char *substr(char *s,int startloc,int len);
int main()
{
char str[80];
int s=0,l=0;//精度出了问题,之前是char的时候转为int直接为0了。
printf("Input string: ");
gets(str);
printf("Start location: ");
scanf("%d",&s);
printf("Substring length:");
scanf("%d",&l);
printf("The substring is :%s\n",substr(str,s,l));
}
char *substr(char *s,int startloc,int len)
{
static char sstr[80];
int i;
if((startloc<0)||(startloc>=strlen(s))||(len<0))
return (NULL);
for(i=0; i<len && s[startloc+i]!='\0';i++)
sstr[i]= s[startloc+i];
sstr[i]='\0';
return (sstr);
}
编写一函数strlshif(char*s,int n),其功能是把字符串s中的所有字符左移n个位置,字符串中的前n个字符移到最后
#include<stdio.h>
#include<string.h>
void strlshif(char *s,int n);
int main()
{
char str[]="0123456789";
strlshif(str,3);
printf("%s\n",str);
}
void strlshif(char *s,int n)
{
int i,len;
char ch;
len=strlen(s);
for(i=0;i<n;i++)
{
ch=s[0];
strncpy(s,s+1,len-1);
s[len-1]=ch;
}
}
编写一函数fun,它的功能是:删除字符串中的数字字符。
#include<stdio.h>
#include<string.h>
void func(char *s);
int main()
{
char str[80];
printf("Input string:");
gets(str);
func(str);
printf("After delete digital char:%s\n",str);
}
void func(char *s)
{
char *pstr;
for(pstr=s;*pstr!='\0';pstr++)
{
while(*pstr>='0'&&*pstr<='9')
strcpy(pstr,pstr+1);//相当于覆盖
}
}
编写一个函数totsubstrnum(char *str,char *substr),它的功能是:统计子字符串substr在字符串str中出现的次数
#include<stdio.h>
#include<string.h>
int totsubstrnum(char *str,char *substr);
int main()
{
char str[80];
char substr[80];
printf("Input string: ");
gets(str);
printf("Input substring: ");
gets(substr);
printf("count=%d\n",totsubstrnum(str,substr));
}
int totsubstrnum(char *str,char *substr)
{
int i=0,count=0,len1,len2;
len1=strlen(str);
len2=strlen(substr);
while(i<=len1-len2)
{
if(strncmp(str+i,substr,len2)==0)
{
count++;
i+=len2;
}
else
i++;
}
return(count);
}
编写一带命令行参数的程序输出星期一到星期日的英文名
这是什么玩意,要多看
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int GetWeek(char *str);
char *week_str[]={"sunday","monday","tuesday","wednesday","thursday","friday","saturday"};
int main(int argc,char *argv[])
{
int k;
if(argc!=2)
{
printf("Use this program like this:example9-8 3");
exit(0);
}
k=GetWeek(argv[1]);
if(k==-1)
{
printf("command line argument must be in [0..6]\n");
exit(0);
}
printf("%s\n",week_str[k]);
}
int GetWeek(char *str)
{
int k,i=0;
while(str[i]!=0)
{
if(str[i]<'0'||str[i]>'9')
return(-1);
else
i++;
}
k=atoi(str);
if(k<0||k>6)
return(-1);
return(k);
}
编写一程序用于计算任意大的两整数之差
太多了,先放着