程序设计之字符串1
字符串
在字符串操作后切记最后得加上字符串结束标识符
′
\
0
′
'\backslash0'
′\0′ 。
问题1_1
函数
f
u
n
fun
fun 的功能是:计算
s
s
s 所指字符串中含
t
t
t 所字符串的数目,并作为函数值返回。
从字符串
s
s
s 中找子字符串的方法是:从第一个字符开始,对字符串进行遍历,若
s
s
s 串的当前字符等于
t
t
t 串的第一个字符,两个字符串的指针自动加
1
1
1 ,继续比较下一个字符;若比较至字符串
t
t
t 的末尾,则跳出循环;若
s
s
s 串的字符与
t
t
t 串的字符不对应相同,则继续对
s
s
s 串的下一个字符进行处理。
代码1_1
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define N 80
int fun(char *s, char *t){
int n;
char *p, *r;
n = 0;
p = &s[0];
r = t;
while(*p){
if(*r==*p){
r++;
if(*r=='\0'){
n++;
r = t; // r=&t[0]
}
}
p++;
}
return n;
}
void main(void){
char a[N], b[N];
int m;
printf("\nPlease enter string a:");
gets(a);
printf("\nPlease enter substring b:");
gets(b);
m = fun(a, b);
printf("\nThe result is: m=%d\n", m);
}
结果1_1
问题1_2
函数
f
u
n
fun
fun的功能是:将形参
s
s
s 所指字符串中下标为奇数的字符取出,并按
A
S
C
I
I
ASCII
ASCII 码大小递增排序,将排序后的字符存入形参
p
p
p 所指字符数组中,形成一个新串 。
例如: 形参
s
s
s 所指的字符为
“
b
a
a
w
r
s
k
j
g
h
z
l
i
c
d
a
”
“baawrskjghzlicda”
“baawrskjghzlicda” ,执行后
p
p
p 所指字符数组中的字符串应为
"
a
a
c
h
j
l
s
w
"
"aachjlsw"
"aachjlsw" 。
代码1_2
#include<stdio.h>
void fun(char *s, char *p){
int i, j, n, x, t;
n = 0;
for(i=0; s[i]!='\0'; i++)
n++;
for(i=1; i<n-2; i++){
t = i;
for(j=i+2; j<n; j=j+2){
if(s[t]>s[j])
t = j;
}
if(t!=i){
x = s[i];
s[i] = s[t];
s[t] = x;
}
}
for(i=1,j=0; i<n; i=i+2,j++)
p[j] = s[i];
p[j] = '\0';
}
void main(void){
char s[80] = "baawrskjghzlicda", p[50];
printf("\nThe original string is:%s\n", s);
fun(s, p);
printf("\nThe result is:%s\n", p);
}
结果1_2
问题1_3
函数
f
u
n
fun
fun的功能是:在形参
s
s
s 所指字符串中寻找与参数
c
c
c 相同的字符,并在其后插入一个与之相同的字符,若找不到相同的字符则不做任何处理。
例如: 形参
s
s
s 所指的字符串为
“
b
a
a
c
d
a
”
“baacda”
“baacda” ,
c
c
c 中的字符为
a
a
a ,执行后
s
s
s 所指的字符串应为
"
b
a
a
a
a
c
d
a
a
"
"baaaacdaa"
"baaaacdaa" 。
代码1_3
#include<stdio.h>
void fun(char *s, char c){
int i, j, n;
for(i=0; s[i]!='\0'; i++){
if(s[i]==c){
n = 0;
while(s[i+1+n]!='\0')
n++;
for(j=i+n+1; j>i; j--)
s[j+1] = s[j];
s[j+1] = c;
i = i+1;
}
}
}
void main(void){
char s[80] = "baacda", c;
printf("\nThe string: %s\n", s);
printf("\nInput a character:");
scanf("%c", &c);
fun(s, c);
printf("\nThe reuslt is: %s\n", s);
}
结果1_3
问题1_4
函数
f
u
n
fun
fun的功能是:在形参
s
s
s 所指字符串中所指的字母字符前移,其它字符顺序后移,处理后将新字符串的首地址作为函数值返回 。
例如: 形参
s
s
s 所指的字符串为
“
a
d
c
123
d
e
f
456
g
h
”
“adc123def456gh”
“adc123def456gh” ,执行后
s
s
s 所指的字符串应为
"
a
b
c
d
e
f
g
h
123456
"
"abcdefgh123456"
"abcdefgh123456" 。
代码1_4
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char *fun(char *s){
int i, j, k, n;
char *p, *t;
n = strlen(s)+1;
t = (char*)malloc(n*sizeof(char));
p = (char*)malloc(n*sizeof(char));
j = 0;
k = 0;
for(i=0; i<n; i++){
if(((s[i]>='a')&&(s[i]<='z'))||((s[i]>='A')&&(s[i]<='Z'))){
t[j] = s[i];
j++;
}
else{
p[k] = s[i];
k++;
}
}
for(i=0; i<k; i++){
t[j+i] = p[i];
}
t[j+k] = '\0'; // 或 t[j+k] = 0;
return t;
}
void main(void){
char s[80];
printf("Please input:");
scanf("%s", s);
printf("\nThe result is: %s\n", fun(s));
}
结果1_4
问题1_5
函数
f
u
n
fun
fun的功能是:在形参
s
s
s 所指字符串中最后一次出现的
t
1
t1
t1 所指字符串相同的子串替换成
t
2
t2
t2 所指字符串,所形成的新串放在
w
w
w 所指的数组中。要求
t
1
t1
t1 和
t
2
t2
t2 所指字符串的长度相同。
例如: 形参
s
s
s 所指的字符串为
“
a
b
c
d
a
b
f
a
b
c
”
“abcdabfabc”
“abcdabfabc” ,
t
1
t1
t1 所指串中的内容为
"
a
b
"
"ab"
"ab" ,
t
1
t1
t1 所指串中的内容为
"
99
"
"99"
"99" 时,结果在
w
w
w 所指的数组中的内容应为
“
a
b
c
d
a
b
f
99
c
”
“abcdabf99c”
“abcdabf99c” 。
代码1_5
#include<stdio.h>
#include<conio.h>
#include<string.h>
void fun(char *s, char t1[], char t2[], char w[]){
char *p, *r, *a;
strcpy(w, s);
while(*w){
p = w;
r = t1;
while(*r){
if(*r==*p){
r++;
p++;
}
else{
break;
}
}
if(*r=='\0'){
a = w;
}
w++;
}
r = t2;
while(*r){
*a = *r;
a++;
r++;
}
}
void main(void){
char s[100], t1[100], t2[100], w[100];
printf("\nPlease enter string S:");
scanf("%s", s);
printf("\nPlease enter substring t1:");
scanf("%s", t1);
printf("\nPlease enter substring t2:");
scanf("%s", t2);
if(strlen(t1)==strlen(t2)){
fun(s, t1, t2, w);
printf("\nThe result is:%s\n", w);
}
else{
printf("\nError: strlen(t1)!=strlen(t2)\n");
}
}
结果1_5
问题1_6
函数
f
u
n
fun
fun的功能是:除了字符串的前导和尾部的 * 号外,将串中其它的 * 号全部删除。形成
h
h
h 已指向字符串中第一个字母,形参
p
p
p 指向字符串中最后一个字母。在编写函数时,不得使用
C
C
C 语言提供的字符串函数。
例如: 若字符串中的内容为
∗
∗
∗
∗
∗
A
∗
B
∗
C
D
∗
∗
∗
∗
∗
*****A*B*CD*****
∗∗∗∗∗A∗B∗CD∗∗∗∗∗ ,删除之后,内容应为
∗
∗
∗
∗
∗
A
B
C
D
∗
∗
∗
∗
∗
*****ABCD*****
∗∗∗∗∗ABCD∗∗∗∗∗ 。
代码1_6
#include<stdio.h>
#include<conio.h>
#include<string.h>
void fun(char *a, char *h, char *p){
int i=0;
char *q=a;
while(q<h){
a[i] = *q;
q++;
i++;
}
while(q<p){
if(*q!='*'){
a[i] = *q;
i++;
}
q++;
}
while(*q){
a[i] = *q;
i++;
q++;
}
a[i] = '\0';
}
void main(void){
char s[81], *t, *f;
printf("Enter a string : \n");
gets(s);
t = f = s;
while(*t)
t++;
t--;
while(*t=='*')
t--;
while(*f=='*')
f++;
fun(s, f, t);
printf("The string after deleted: \n");
puts(s);
}
结果1_6
问题1_7
函数 f u n fun fun的功能是:使字符串的前导 * 号不多于 n n n 个,中间和尾部的 * 号不动,在编写函数时,不得使用 C C C 语言提供的字符串函数。
代码1_7
#include<stdio.h>
#include<conio.h>
void fun(char *a, int n){
int i=0, k=0;
char *p, *t;
p = t = a; // p和t同时指向 a的首地址
while(*t=='*'){
k++;
t++;
}
if(k>n){
while(*p){
a[i] = *(p+k-n);
i++;
p++;
}
a[i] = '\0';
}
}
void main(void){
char s[81];
int n;
printf("Enter a string: \n");
gets(s);
printf("Enter n:");
scanf("%d", &n);
fun(s, n);
printf("The string after deleted: \n");
puts(s);
}
结果1_7
问题1_8
函数 f u n fun fun的功能是:在形参 s s ss ss 所指字符串数组中,将所有串长超过 k k k 的字符串中后面的字符删除,只保留前面的 k k k 哥字符。 s s ss ss 所指字符串中共有 N N N 个字符串,且串长小于 M M M 。
代码1_8
#include<stdio.h>
#include<string.h>
#define N 5
#define M 10
void fun(char (*ss)[M], int k){
int i=0;
while(i<N){
ss[i][k] = '\0';
i++;
}
}
void main(void){
char x[N][M] = {"Zhangsan", "Lisi", "Wangwu", "Qianliu", "Zhiyi"};
int i;
printf("\nThe original string\n\n");
for(i=0; i<N; i++)
puts(x[i]);
printf("\n");
fun(x, 4);
printf("\nThe string after deleted:\n\n");
for(i=0; i<N; i++)
puts(x[i]);
printf("\n");
}