?? ruanjian.cpp
字號:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 50
#define M 100
void main()
{
int x;
char c;
char s;
char str[M];
int letters=0;
int space=0;
int digit=0;
int others=0;
int n,m,temp,i,j,k,num[N];
int l=0;
char cc,n1;
int w=0;
while(1)
{
printf("\n\n 1,對整數數組排序 \n");
printf(" 2,計算文本中非空格字符的個數\n");
printf(" 3,用單個空格代替文本行中的所有空格 \n");
printf(" 4,連接字符串及求字符串長度和取子字符串\n");
printf(" 5,退出\n");
scanf("%c",&s);
getchar();
if(s=='1')
{
printf("請輸入要排序的數組:\n");
scanf("%d",&num[w]);
while(1)
{
if(((cc=getchar())==' '))
{
w++;
scanf("%d",&num[w]);
}
else
{
w++;
break;
}
}
for(i=0;i<w;i++)
{
for(j=0;j<w-(i+1);j++)
{
if(num[j+1]<num[j])
{
temp=num[j+1];
num[j+1]=num[j];
num[j]=temp;
}
}
}
for(m=0;m<w;m++)
printf(" %d ",num[m]);
printf("\n");
// exit(0);
w=0;
}
if(s=='2')
{
printf("請輸入要計算的文本:\n");
// getchar();
// loop:
while((c=getchar())!='\n')
{
if((c>='a'&& c<='z')||(c>='A'&&c<='Z'))
letters++;
else if(c>='0'&&c<='9')
digit++;
else if(c==' ')
space=0;
else
others++;
}
x=letters+digit+others;
printf("所求文本非空格字符的長度為:%d \n",x);
letters=0;
space=0;
digit=0;
others=0;
// printf( "請輸入要計算的文本:");
//goto loop;
//exit(0);
}
if(s=='3')
{
char str0[M];
int z ;
int j=0;
int k,space;
int lenth;
printf("請輸入帶有空格的字符串:\n");
gets(str0);
lenth = strlen(str0);
while(str0[j]!='\0')
{
if(str0[j]==' ')
{
space=0;
//count how many space need to delete
for(k=j+1;k<lenth; k++ )
{
if(str0[k]==' ')
space++;
else
break;
}
if(space>0)
{
//move the non-space character to the space
for(k=j+space+1; k<lenth; k++)
{
str0[k-space] = str0[k];
}
//replace the txt in the last with '\0'
for(k=lenth-1; k>lenth-space; k--)
str0[k] ='\0';
}
}
j++;
}
printf("%s \n", str0);
// exit(0);
}
if(s=='4')
{
char str1[M];
char str2[M];
printf("請輸入字符串 1:\n");
gets(str1);
printf("%d\n",strlen(str1));
printf("請輸入字符串 2:\n");
gets(str2);
printf("%d\n",strlen(str2));
printf("%s\n",strcat(str1,str2));
printf("%d\n",strlen(str1));
printf("請輸入你要選擇子字符串的范圍n<n1-n>=");
scanf("%d",&n1);
scanf("%d",&n);
l=strlen(str1);
// strcpy(str[M],str1[M]);
// printf("%d\n",l);
if(n>l)
printf("你選擇的字符超出了所輸入的總長度!!!!!!\n");
else
{
for(k=n1;k<=n;k++)
{
printf("%c",str1[k]);
}
}
printf("\n");
}
if(s=='5')
{
exit(0);
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -