C程序读取字符串并按字母顺序打印

这段C程序读取用户输入的字符串,并使用冒泡排序算法将其字符按字母顺序进行打印。程序首先读取字符串,然后通过两层循环比较相邻字符,如果前一个字符大于后一个字符,则交换它们的位置。经过遍历,最终打印出排序后的字符串。
摘要由CSDN通过智能技术生成
C program to read a string and print it in alphabetical order

#include<stdio.h> #include<conio.h> #include<string.h>

#include <stdio.h> #include <conio.h> #include <string.h>

void main() { int i,j,n,ch1,ch2; char a[50],temp; clrscr(); printf(“Enter any string:”); scanf(“%s”,a); n=strlen(a);

void main(){int i,j,n,ch1,ch2; 字符a [50],temp; clrscr(); printf(“输入任意字符串 :”); scanf(“%s”,a); n = strlen(a);

for(i=1;i<n;++i) for(j=0;j<(n-i);++j) { ch1=a[j]; ch2=a[j+1]; if(ch1>ch2) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } }

for(i = 1; i <n; ++ i)for(j = 0; j <(ni); ++ j){ch1 = a [j]; ch2 = a [j + 1]; if(ch1> ch2){temp = a [j]; a [j] = a [j + 1]; a [j + 1] = temp; }}

printf(“String after arranging %s”,a); getch(); }

printf(“安排%s后的字符串”,a); getch(); }

翻译自: https://www.thecrazyprogrammer.com/2013/05/c-program-to-read-string-and-print-it.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值