c语言题目,字符串的基本 *** 作(5个基本函数实现)

c语言题目,字符串的基本 *** 作(5个基本函数实现),第1张

#include <stdioh>

#define MAXN 100

typedef enum { fail, success } status;

typedef enum { false, true} boolean;

char s[MAXN], s1[MAXN], s2[MAXN];

void scopy(char s1[],char s2[])

{int i=0;

while(s2[i]!='\0')

{ s1[i]=s2[i];

i++;

}

s1[i]='\0';

}

int strlen(char s[] )

{ int i;

for (i=0; s[i]!='\0'; i++ );

return (i);

}

status strcat(char s1[],char s2[])

{int i, j, k;

if ((i=strlen (s1)) + (j=strlen (s2) )>=MAXN)

return (fail);

for (k=0; k<=j; k++ )

s1[i+k]=s2[k];

return (success);

}

status strins(char s1[], int i, char s2[ ])

{int m, n, k;

if (i<0||i>(m=strlen(s1))||m+(n=strlen(s2))>MAXN)

return(fail);

if(m+n<=MAXN){

for(k=m+n-1;k>=n+i;k--)

s1[k]=s1[k-n];

for(k=0;k<n;k++)s1[k+i]=s2[k];

m=m+n;

}

else if(k+n<=MAXN){

for(k=MAXN-1;k>n+i-1;k--)s1[k]=s1[k-n];

for(k=0;k<n;k++)s1[k+i]=s2[k];

m=MAXN;

}

else{

for(k=0;k<MAXN-i;k++)s1[k+i]=s2[k];

m=MAXN;

}

return(success);

}

status strsub (sl, i, j, s2)

char sl[ ], s2[ ];

int i,j;

{ int m, k;

if (i<0 || i>= (m=strlen(sl)))

return (fail );

if (j<0 || i+j>m)

return (fail );

else{

for(k=0;k<j;k++)s2[k]=s1[k+i];

j=strlen(s2);

return (success);

}

}

void main( )

{ int i;

i=strlen("aassddd") ;

printf("%d\n",i);

scopy(s1,"abcd");

scopy(s2,"hijkl");

printf("%s\n",s1);

printf("%s\n",s2);

strcat(s1,s2);

printf("%s\n",s1);

printf("%s\n",s2);

strins(s1,4,s2);

printf("%s\n",s1);

printf("%s\n",s2);

}

#include <stdioh>

int strcmp(char s1, char s2)

{

for (; s1 == s2; s1++, s2++)

if ('\0' == s1)

return 0;

return s1 - s2;

}

int main(void)

{

char s1 = "her";

char s2 = "herb";

printf("%d\n", strcmp(s1, s2));

return 0;

}

#include

void fun(char s)

{ int i, n, k; char c;

n=0;

for(i=0; s[i]!='\0'; i++) n++;

/found/

if(n%2==0) k=n-___1___ ;

else k=n-2;

/found/

c=___2___ ;

for(i=k-2; i>=1; i=i-2) s[i+2]=s[i];

/found/

s[1]=___3___ ;

}

main()

{ char s[80]="abcdefgh";

printf("\nThe original string is : %s\n",s);

fun(s);

printf("\nThe result is : %s\n",s);

}

自己填空吧

#include <stdioh>

#define MAXN 1000

char a[ MAXN ];

int main()

{

int words, numbers , flags , blanks ;

printf("请输入一串连续的字符:\n");

while( gets(a) )

{

words = 0;

numbers = 0;

flags = 0;

blanks = 0;

for( int i = 0 ; a[i]!='\0' ; i ++ )

{

if( (a[i] >= 'a' && a[i] <= 'z') || ( a[i] >= 'A' && a[i] <= 'Z' ) )

{

words++;

}

else if( a[i] >='0' && a[i]<='9' )

{

numbers++;

}

else if( a[i] == ' ' )

{

blanks++;

}

else

{

flags++;

}

}

printf("字母个数 %d\n数字个数 %d\n空格个数 %d\n其他字符个数 %d\n\n",words,numbers,blanks,flags);

printf("请输入一串连续的字符:\n");

}

return 0;

}

#include<stdioh>

#include<stringh>

#define MAX 80

void delechar(char str[],char a)

{

int i=0,j;

while(str[i]!='\0')

{

if(str[i]==a)

{

for(j=i;str[j]!=0;j++)

{

str[j]=str[j+1];

}

}

i++;

}

}

int main(void)

{

int i;

char a,str[MAX];

gets(str);

scanf("%c",&a);

delechar(str,a);

puts(str);

return 0;

}

程序如下:

main(){

int c;

char s[100],p;

char a='a',z='z';

scanf("%s",s);

for(c=0,p=s;p;p++)

if(p>=a&&p<=z)

++c;

printf("字符串%s中共有小写字母%c个。\n",a,c);

}

有什么问题请留言。

#include <stdioh>

void mystrcpy(char str1[],char str2[],int m,int n);

int main()

{

char str1[100],str2[100];

int m,n;

printf("Input str1:\n");

gets(str1); printf("Input m and n:\n");

scanf("%d%d",&m,&n);

mystrcpy(str1,str2,m,n);

puts(str2);

return 0;}

void mystrcpy(char str1[],char str2[],int m,int n)

{

int i=m-1,j=0;

for(j=0;j<n;j++)

{

str2[j]=str1[i];

if(i!=n-1)i++;

}

str2[n]='\0';

}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/11671027.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存