1. 编写一个函数find(s,c)，在给定字符串s中查找字符c，返回c在s中第一次出现时所在的地址。如果找不到，返回NULL。

```   1 /*sample code*/
2 char *find(char *s, int c) {
3    for(;*s!='\0';s++)
4       if(*s==c)
5          return s;
6    return NULL;
7 }
```

```int find(char s[], int c) {
int i;
for(i = 0; s[i]!='\0'; i++)
s[i]!=c;
return i;
}
return 0;
}

find(char s[], char c)
{
int i;
for(i=0; s[i] != '\0';) {
if(s[i] = c)
return c;
else i++;
}
return NULL;
}

void find(s, c) {
int *p;
while((s=getchar())!=EOF) {
for(p=0; *p!=c; p++)
if *p = c
return p;
else
return NULL;
}
}

void find(char s[], c) {
int i, j;
for(i=0; i<j; i++) {
s[i] = c;
i++;
}
return NULL;
}

int find(char s[], int c, int i) {
int c, i;
char s[];
for(i = 0; s[i] !='\0'; i++);
if(i==c);
return i;
else
return NULL
}

void find(char *s, char c) {
for(; *s!='\0'; s++){
if(*s==c)
return s;
else
return NULL;
}
}

int find(char s[], char c[]) {
int i, j;
for(i=0; i!='\0'; i++) {
for(j=i; s[j]==c[k]; j++,k++);
if(c[k] ='\0' && k>0)
return i;
}
return NULL;
}```

1. 写一个函数transpose(a)转置方阵a。a是一个N行N列的二维数组。N为常数。

```   1 /*sample code*/
2 int transpose(int a[N][N])
3 {
4    int i, j;
5    for(i=0; i<N; i++) {
6       for(j=i+1; j<N; j++) {
7          int temp = a[i][j];
8          a[i][j] = a[j][i];
9          a[j][i] = temp;
10       }
11    }
12 }
```

```int transpose(int a[N][N]) {
int i, j, temp;
for(i = 0; i < N; i++)
for(j = 0; j < N; j++) {
temp = s[i][j];
s[i][j] = s[j][i];
s[j][i] = temp;
}
}

int transpose(int a[N][N]) {
int i, j, b;
for(i = 0; i < N; i++)
for(j = 0; j < i; j++) {
b= a[i][j];
a[i][j] = a[j][i];
a[j][i] = b;
}
}

int transpose(int a[N][N])
{
int i, j;
for(i=0; i<N-1; i++)
for(j=0; j<N-1; j++)
a[i][j] = a[j][i];
}```

```   1 /*sample code*/
2 struct complex {
3    double real;
4    double imag;
5 };
6
7 struct complex add(struct complex c1, struct complex c2)
8 {
9    c1.real += c2.real;
10    c1.imag += c2.imag;
11    return c1;
12 }
```

```struct complex {
int i;
int j;
} c1, c2;

struct complex add(struct complex c1, struct complex c2)
{
c1.i += c2.i;
c1.j += c2.j;
return c1;
}

struck complex{
int a;
int i;
} c1, c2;

struck complex add(struck complex c1, struck complex c2)
{
struck complex c3;
c3->a = c1->a + c2->a;
c3->i = c1->i + c2->i;
return struck complex c3;
}```

1. 给定strfilter函数，给定main函数，请写isnotalnum函数，使得程序能够删除字符串中非数字非英文字母的字符。

```   1 /*sample code*/
2 void strfilter(char s[], int (*fun)(int) ) {
3    int i, j;
4    for(i=j=0; s[i]!='\0'; i++)
5       if( !(*fun)(s[i]) )
6          s[j++] = s[i];
7    s[j] = '\0';
8 }
9 main() {
10    char str[]="Hello, 123";
11    strfilter(str, isnotalnum);/*Hello123*/
12    printf("%s\n", str);
13 }
```

```   1 int isnotalnum(int c)
2 {
3    return !isdigit(c) && !isalpha(c);
4 }
```

```void isnotalnum(char s[]) {
int i, j;
for(i=j=0; s[i] != '\0'; i++)
if(isalnum(s[i]))
s[j++] = s[i];
s[j] = '\0';
}

int isnottalnum(int c)
{
if(c>='0' || c=<'9')
return 1;
else if(c>='a' || c<='z')
return 1;
else if(c>='A' || c<=Z)
return 1;
else
return -1;
}```

1. 写一个程序，输出所有命令行参数中最大的一个。比如程序名字为max，输入命令max aa cc dd bb运行程序，程序输出dd。

```   1 /*sample code*/
2 #include <stdio.h>
3
4 int main(int argc, char *argv[])
5 {
6    int i, max = 1;
7    for(i = 2; i < argc; i++)
8        if(strcmp(argv[i], argv[max])>0)
9            max = i;
10    if( max < argc)
11        printf("%s", argv[i]);
12 }
```

```#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
int i, j;
i = 1;
while(i<argc) {
for(j=i; j<argc;j++) {
if(strcmp(argv[i], [j])<0)
i = j;
break;
}
return i;
}
printf("%s", argv[i]);
}

int main(int argc, char *argv[]) {
if(argc > 1)
qsort(v, 1, argc);
printf("%s", argv[argc-1]);
printf("\n");
return 0;
}

int main(int argc, char *argv[]) {
int i;
char *t;
if(argc!=1) {
t = argv[1];
for(i=2; i<argc; i++) {
if(strcmp(t,argv[i])<0 )
t = argv[i];
}
printf("%s", t);
}
}

#define MAX 500
int main(int argc, char *argv[]) {
char s[MAX];
strcpy(s, argv[1]);
int i;
while(i=2; i<argc-1; i++)
if(strcmp(s, argv[i]) <0)
strcpy(s, argv[i]);
if(argc != 1)
printf("%s", s);
return 0;
}```

C语言练习4 (last edited 2008-02-23 15:34:09 by localhost)

ch3n2k.com | Copyright (c) 2004-2022 czk.