编程素材简单(编程猫ut素材)

2023-11-22 09:11:27 数码极客 bianji01

 

C语言编程题,求助高手!!!

/*用scanf()无法输入空格,就用getchar了*/#includeint main(void)char ch;static char str[128];int i=0;int j=0;printf("input the string: \n");while(1)ch=getchar();if(ch==\n||i>127)break;str[i]=ch;for(i=0;str[i];i++)for(j=i+1;str[j];j++)if(str[i]>str[j])ch=str[i];str[i]=str[j];str[j]=ch;printf("\nResult:%s\n",str);return 0;#include #define MAX 127void swap_bubble(char arr[], int n)int i,j,temp;for (i = 0; i < n-1; ++i)//是否有过交换bool is_changed = false;for (j = 0 ; j < n - i -1; ++j){//字符比较本身即是按每个字符的ASCII码进行比较的if (arr[j+1] < arr[j]){//如果后面的比前面的小 交换两者位置temp = arr[j];arr[j] = arr[j+1];arr[j+1] = temp;is_changed = true;}}if (!is_changed){//没有过交换 证明已排序完成 退出break;}void main()int length = 0; //记录字符串长度char input_array[MAX];printf("input the string: (Ctrl+Z to end)\n");scanf("%s",input_array);//获取字符串长度while(input_array[length]!=\0)length++;//排序swap_bubble(input_array,length);printf("\nResult:");//输出结果for (int i = 0; i < length; ++i)printf("%c ",input_array[i]);printf("\n");//此句没用 防止屏幕一闪而过 可以注释//可以换成 getchar()或者system("pause")scanf("%d",&length);不好意思啊,这次改好了 这个可以,纯C的 用吧 没有C++头文件了#include#include void main()char temp[127];for ( int n = 0; n < 127; ++n )temp[n] = \0;scanf("%s",temp);printf("\n排序前:%s\n", temp );for ( int i = 0; temp[i] != \0; ++i )for ( int j = 0; temp[j] != \0; ++j )if ( temp[i] > temp[j] )char tt = temp[i];temp[i] = temp[j];temp[j] = tt;printf("排序后:%s\n", temp );这可是纯C程序哦!/*---------------------------------------------------------------------------*/#include #include #include int cmp(const void *a,const void *b)return *(const char *)a-*(const char *)b;int main(void)char str[128];int i=0;printf("please input a string:\n");for (i = 0; (str[i]=getchar())!=\n; i++);str[i]=\0;printf("input the string: \n");printf("%s",str);qsort(str,strlen(str),sizeof(char),cmp);printf("\nResult:");printf("\n%s\n",str);return 0;/*---------------------------------------------------------------------------*/不能用fgets,为什么?

C语言编程题,输入5个字符串,然后排序

#defineLINEMAX20/*定义字符串的最大长度*/

intmain()

{inti;

char**p,*pstr[5],str[5][LINEMAX];

for(i=0;i<5;i++)

pstr<i>=str<i>;/*将第i个字符串的首地址赋予指针数组pstr的第i个元素*/

printf("input5strings:\n");

for(i=0;i<5;i++)

scanf("%s",pstr<i>);

p=pstr;

sort(p);

printf("stringssorted:\n");

for(i=0;i<5;i++)

printf("%s\n",pstr<i>);

sort(char**p)/*冒泡法对5个字符串排序函数*/

{inti,j;

char*temp;

for(i=0;i<5;i++)

{for(j=i+1;j<5;j++)

{if(strcmp(*(p+i),*(p+j))>0)/*比较后交换字符串地址*/

{temp=*(p+i);

*(p+i)=*(p+j);

*(p+j)=temp;

return0;

扩展资料:

printf()函数的调用格式为:printf("<格式化字符串>",<参量表>)。

其中格式化字符串包括两部分内容:一部分是正常字符,这些字符将按原样输出;另一部分是格式化规定字符,以"%"开始,后跟一个或几个规定字符,用来确定输出内容格式。

参量表是需要输出的一系列参数,其个数必须与格式化字符串所说明的输出参数个数一样多,各参数之间用","分开,且顺序一一对应,否则将会出现意想不到的错误。

inta=1234;

printf("a=%d\n",a);

输出结果为a=1234。

scanf()是C语言中的一个输入函数。与printf函数一样,都被声明在头文件stdio.h里,因此在使用scanf函数时要加上#include<stdio.h>。

intscanf(constchar*restrictformat,...);

函数scanf()是从标准输入流stdin(标准输入设备,一般指向键盘)中读内容的通用子程序,可以说明的格式读入多个字符,并保存在对应地址的变量中。

scanf("%d%d",&a,&b);

函数返回值为int型,如果a和b都被成功读入,那么scanf的返回值就是2。

声明:易趣百科所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系315127732@qq.com
广告位招租
横幅广告