当前位置:首页 > 问答大全 > 求C语言程序,高手请进...

求C语言程序,高手请进...

被浏览: 0次 2023年09月03日 22:19
热门回答(4个)
游客1

/*1.输入一个以回车结束的字符串(少于80个字符),将其中的大写字母用下面列出的对应大写字母替换,其余字符不变,输出替换后的字符串
原字母 对应字母
A Z
B Y
C X
D W

X C
Y B
Z A*/

/*#include
void main()
{
char str[80];
char a;
int i=0;
scanf("%c",&a);
while(a!='\n')
{
if(a>='A'&&a<='Z')
str[i++]='A'+'Z'-a;
else
str[i++]=a;
scanf("%c",&a);
}
str[i]=0;
printf("%s\n",str);
}*/

/*2.删除字符串中的字符
输入一个字符串s,再输入一和虚个字符c,将字符串s中出现的所有字符c删除。
要求定义并调用函数delchar(s,c),它的功能是将字符串中的出现的所有c字符删除
输入输出示例
Input a string: happy new year
Input a char: a
After deleted, the string is: hppy new yer*/
/*#include
#include
void delchar(char *s,char c)
{
char tmp[255];
int i=0,j=0;
for(;i if(s[i]!=c)
tmp[j++]=s[i];
tmp[j]=0;
strcpy(s,tmp);
}
void main()
{
char s[255],c;
printf("Input a string:");
gets(s);
printf("Input a char:");
scanf("%c",&c);
delchar(s,c);
printf("After deleted, the string is:%s\n",s);
}*/
/*3.字符串复制。输入一个字符串t和一个正整数m,将字符串t中从第m个字符开始的全部字符复制到字符串s中,再输出字符串s
。要求用字符指针定义并调用函数strmcpy(s,t,m),它的功能是将字符串t中从第m个字符开始的全字符复制到字符串s中。
输入输出示例
Input a string: happy new year.
Input an integer: 7
Output is: new year. */
/*#include
#include
void strmcpy(char *s,char *t,int m)
{
int i,j=0;
i=m-1;
while(t[i])
s[j++]=t[i++];
s[j]=0;
}
void main()
{
char s[255],t[255];
int m;
printf("Input a string:");
gets(t);
printf("Input a integer:");
scanf("%d",&m);
strmcpy(s,t,m);
printf("Output is:%s\n",s);
}*/

/*4.编程判断输入的一串字符是否为“回文”。所谓“回文”,是指顺读和倒读都是一样的字符串。如”XYZYX”和”xyzyx”,都是“回文”。
输入输出示例:
第一次运行:
Input a string: abcddcba
YES
第二次运行:
Input a string:abcddcb
NO*/
/*#include
#include
int isHunwen(char *s)
{
int i;
for(i=0;i if(s[i]!=s[strlen(s)-1-i])
return 0;
return 1;
}
void main()
{
char s[255];
printf("Input a string:");
gets(s);
if(isHunwen(s))
printf("YES\n");
else
printf("NO\n");
}*/
/*5.输入一行文字,统计其中的大写字母、小写字母、空格、数字以及其他字符的个数。

输入输出示例:
Input a string: bFaE3+8=1 B
大写字母的个数为:3
小写字母的个银棚悔数为:2
空格的个锋正数为:1
数字的个数为:3
其他字符的个数为:2 */

#include
#include
#include
void main()
{
char s[255];
int i=0,up=0,low=0,space=0,num=0,other=0;
printf("Input a string:");
gets(s);
while(s[i])
{
if(isupper(s[i]))
up++;
else if(islower(s[i]))
low++;
else if(isspace(s[i]))
space++;
else if(isdigit(s[i]))
num++;
else
other++;
i++;
}
printf("大写字母的个数为:%d\n",up);
printf("小写字母的个数为:%d\n",low);
printf("空格的个数为:%d\n",space);
printf("数字的个数为:%d\n",num);
printf("其他字符的个数为:%d\n",other);
}

游客2

/*1.输入一个以回车结束的字符串(少于80个字符),将其中的大写字母用下面列出的对应大写字母替换,其余字符不变,输出替换后的字符串
原字母
对应字母
A
Z
B
Y
C
X
D
W
X
C
Y
B
Z
A*/
/*#include

void
main()
{
char
str[80];
char
a;
int
i=0;
scanf("%c",&a);
while(a!='\n')
{
if(a>='A'&&a<='Z')
str[i++]='A'+'Z'-a;
else
str[i++]=a;
scanf("%c"和虚,&a);
}
str[i]=0;
printf("%s\n",str);
}*/
/*2.删除字符串中的字符
输入一个字符串s,再输入一个字符c,将字符串s中出现的所有字符c删除。银棚悔
要求定义并调用函数delchar(s,c),它的功能是将字符串中的出现的所有c字符删除
输入输出示例
Input
a
string:
happy
new
year
Input
a
char:
a
After
deleted,
the
string
is:
hppy
new
yer*/
/*#include

#include

void
delchar(char
*s,char
c)
{
char
tmp[255];
int
i=0,j=0;
for(;iif(s[i]!=c)
tmp[j++]=s[i];
tmp[j]=0;
strcpy(s,tmp);
}
void
main()
{
char
s[255],c;
printf("Input
a
string:");
gets(s);
printf("Input
a
char:");
scanf("%c",&c);
delchar(s,c);
printf("After
deleted,
the
string
is:%s\n",s);
}*/
/*3.字符串复制。输入一个字符串t和一个正整数m,将字符串t中从第m个字符开始的全部字符复制到字符串s中,再输出字符串s
。要求用字符指针定义并调用函数strmcpy(s,t,m),它的功能是将字符串t中从第m个字符开始的全字符复制到字符串s中。
输入输出示例
Input
a
string:
happy
new
year.
Input
an
integer:
7
Output
is:
new
year.
*/
/*#include

#include

void
strmcpy(char
*s,char
*t,int
m)
{
int
i,j=0;
i=m-1;
while(t[i])
s[j++]=t[i++];
s[j]=0;
}
void
main()
{
char
s[255],t[255];
int
m;
printf("Input
a
string:");
gets(t);
printf("Input
a
integer:");
scanf("%d",&m);
strmcpy(s,t,m);
printf("Output
is:%s\n",s);
}*/
/*4.编程判断输入的一串字符是否为“回文”。所谓“回文”,是指顺读和倒读都是一样的字符串。如”XYZYX”和”xyzyx”,都是“回文”。
输入输出示例:
第一次运行:
Input
a
string:
abcddcba
YES
第二次运行:
Input
a
string:abcddcb
NO*/
/*#include

#include

int
isHunwen(char
*s)
{
int
i;
for(i=0;iif(s[i]!=s[strlen(s)-1-i])
return
0;
return
1;
}
void
main()
{
char
s[255];
printf("Input
a
string:");
gets(s);
if(isHunwen(s))
printf("YES\n");
else
printf("NO\n");
}*/
/*5.输入一行文字,统计其中的大写字母、小写字母、空格、数字以及其他字符的个数。
输入输出示例锋正:
Input
a
string:
bFaE3+8=1
B
大写字母的个数为:3
小写字母的个数为:2
空格的个数为:1
数字的个数为:3
其他字符的个数为:2
*/
#include

#include

#include

void
main()
{
char
s[255];
int
i=0,up=0,low=0,space=0,num=0,other=0;
printf("Input
a
string:");
gets(s);
while(s[i])
{
if(isupper(s[i]))
up++;
else
if(islower(s[i]))
low++;
else
if(isspace(s[i]))
space++;
else
if(isdigit(s[i]))
num++;
else
other++;
i++;
}
printf("大写字母的个数为:%d\n",up);
printf("小写字母的个数为:%d\n",low);
printf("空格的个数为:%d\n",space);
printf("数字的个数为:%d\n",num);
printf("其他字符的个数为:%d\n",other);
}

游客3

先回答2题:
第3题:
#include
#include
#include
int main(){
char string[100];

void getString(char[100]);//prototype
void deleteBlank(char[100]);
void check(char[100]);

getString(string);//call the functions
deleteBlank(string);
check(string);

return 0;
}
/码液/This function is used to ask for a string from the user
void getString(char string[100]){
printf("This program can judge whether the string you entered is a palindrome.\n\n");
printf("Please enter a string.\n\n");
gets(string);
printf("\n");
printf("The string you entered is ");
puts(string);
printf("\n");
}
//This function is used to delete the blanks in the string
void deleteBlank(char string[100]){
int i,j=0;

int length=strlen(string);

for(i=0;i for(;string[i]==' ';i++){
}
string[j]=string[i];
j++;
}

string[j]='\0';
}
//This function is the main part of the program, it is used to judge whether the string is a palindrome
void check(char string[100]){

struct character{
char ch;
struct character *priorAddr;
};

struct character *tosp=NULL,*temp;

int length=strlen(string);

for(int i=0;i
struct character *newAddr=(struct character *) malloc(sizeof(struct character));

if(newAddr==(struct character*)NULL)
{

printf("Failed to malloc memory for the structure.\n");
exit(1);
}

newAddr->ch=string[i];
newAddr->priorAddr=tosp;
tosp=newAddr;
}

for(i=0;i if(string[i]!=tosp->ch){
printf("The string you entered is not a palindrome.\n\n");
exit(1);
}
temp=tosp;
tosp=tosp->priorAddr;
free(temp);
}
printf("The string you entered is a palindrome.\n\n");
}

第5题:
#include
void main()
{
int A,a,nub,kon,oth,i;
char b[100];
A=a=nub=kon=oth=0;
printf("input a string: ");
gets(b);
for(i=0;b[i]!='\0';i++)
{
if('A'<=b[i]&&'Z'>=b[i])
A++;
else if('a'迟轮物<=b[i]&&'z'桐庆>=b[i])
a++;
else if('0'<=b[i]&&'9'>=b[i])
nub++;
else if(b[i]==' ')
kon++;
else
oth++;
}
printf("大写字母的个数为:%d\n",A);
printf("小写字母的个数为:%d\n",a);
printf("空格的个数为:%d\n",kon);
printf("数字的个数为:%d\n",nub);
printf("其它字符的个数为:%d\n",oth);
}

游客4

/* HELLO.C -- Hello, world */

#include "stdio.h"
#include"string.h"
/*问题1的函数*/
void swap(){
char str[81];
int i=0;
str[81]='\0';
scanf("%s",str);
while(str[i]!='\0'&&i<=80){
if(str[i]>='A'&&str[i]<='D')str[i]='Z'-str[i]+'A';
else if(str[i]>='X'&&str[i]<='Z')str[i]='C'-str[i]+'X';
i++;
}
printf("%s\n",str);
}

/*问题2的函数*/歼孙禅
void delchar(char *s,char c){
int len=strlen(s);
int i,j;
for(i=0;i if(s[i]==c){
for(j=i;j<=len;j++)
s[j]=s[j+1];
s[j]='\0';
len--;
}
}
}
/*问题3的函数*/
void prob_3(){
char *s,*t;
int m;
printf("Input a string:");
gets(t);
printf("Input a integer:");
scanf("%d",&m);
m=strlen(t)-m+1;
strrev(t);
strncpy(s,t,m);
strrev(s);
printf("Output is:%s\n",s);
}

/*问题4的函数*/
void prob_4(){
int i=0,len,flag=0;
char *s;
printf("Input a string:");
gets(s);
len=strlen(s)-1;
while(i if(*(s+i)!=*(s+len-i)){
flag=1;
break;
}
++i;
}
if(flag)printf("NO\n");
else printf("YES\n");
}
/*问题5的函数*/
void prob_5(){
int i=0,num[5]={0},len;
char *s;
printf("Input a string:");
gets(s);
len=strlen(s);
while(i if(*(s+i)>='A'&&*(s+i)<='Z')num[0]++;
else if(*(s+i)>='a'&&*(s+i)<='z')num[1]++;
else if(*(s+i)==' ')num[2]++;
else if(*(s+i)>='0'&&*(s+i)<='9')num[3]++;
else num[4]++;
++i;
}
printf("大写字母的个数为:%d\n",num[0]);
printf("小写字母的个数为:%d\n",num[1]);
printf("空格的个数氏尘为:%d\n",num[2]);
printf("数字的个数为:%d\n",num[3]);
printf("其他字符的个数为:%d\n"凯闹,num[4]);
}
/*主函数*/
main()
{ char c,s[100];
swap();
printf("Input a string:");
gets(s);
printf("Input a char:");
c=getchar();
delchar(s,c);
printf("%s\n",s);
prob_3();
prob_4();
prob_5();
getch();
}
都写一个程序里了。