您的位置:首页 > 编程语言 > C语言/C++

C语言字符串操作

2008-08-02 00:20 381 查看

插入法排序(C语言)

直接插入排序的基本思想是:
当插入第i(i≥1)个对象时,前面的V[0],V[1],…,v[i-1]已经排好序。这时,用v[i]的关键码与v[i-1],v[i-2],…的关键码顺序进行比较,找到插入位置即将v[i]插入,原来位置上的对象向后顺移。

例子:用c实现的插入排序法,先输入10个数,然后利用插入排序法进行排序,将结果输出。

#include<stdio.h>
intmain()
{
inta[10],r[11];
int*p;
inti,j;
for(i=0;i<10;i++)
{
p=&a[i];
scanf("%d",&a[i]);
r[i+1]=a[i];
}
r[0]=1;
for(i=2;i<=10;i++)
{
r[0]=r[i];
j=i-1;
while(r[j]>r[0])
{
r[j+1]=r[j];
j--;
}
r[j+1]=r[0];
}
for(i=1;i<=10;i++)
{
p=&r[i];
printf("%d/t",*p);
}
return0;
}

C++判断字符串是否为回文

#include<iostream.h>
#include<string.h>
intcheck(char*s)
{
char*p1;
char*p2;
intn;
n=strlen(s);
p1=s;
p2=s+n-1;
while(p1<p2)
{
if(*p1++!=*p2--)
{
return0;
}
else
return1;
}
}
intmain()
{
charstr[100];
cout<<"Inputyourstring:";
cin>>str;
if(check(str))
{
cout<<"Thisishuiwen!"<<endl;
}
else
cout<<"Thisnothuiwen!"<<endl;
return0;
}

[数据结构]线性表(严蔚敏版)

//此代码属于原创代码,供大家学习交流。
//此代码实现了严蔚敏版的数据结构中线性表的代码!

#include<stdlib.h>
#include<stdio.h>
typedefintStatus;
typedefintElemType;
#defineTRUE1
#defineFALSE0
#defineOK1
#defineERROR0
#defineINFEASIBLE-1
#defineOVERFLOW-2
#defineLIST_INIT_SIZE100
#defineLISTINCREMENT10
typedefstruct
{
ElemType*elem;
intlength;
intlistsize;
}Sqlist;
StatusInitList_Sq(Sqlist&L)
{
L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!L.elem)
{
exit(OVERFLOW);
}
L.length=0;
L.listsize=LIST_INIT_SIZE;
returnOK;
}
Statusscan(Sqlist&L,inti)
{
for(i=0;i<L.length;i++)
{
printf("%d/t",L.elem[i]);
}
returnOK;
}

(

[数据结构]三元组(严蔚敏版)

//属于原创代码,供大家学习交流。
//此代码实现了严蔚敏版的数据结构中三元组的代码!
#include<stdio.h>
#include<stdlib.h>
#defineTRUE1
#defineFALSE0
#defineOK1
#defineERROR0
#defineINFEASIBLE-1
#defineOVERFLOW-2
typedefintStatus;
typedefintElemType;
typedefElemType*Triplet;
StatusInitTriplet(Triplet&T)
{
ElemTypev1,v2,v3;
T=(ElemType*)malloc(3*sizeof(ElemType));
if(!T)
{
exit(OVERFLOW);
}
scanf("%d%d%d",&v1,&v2,&v3);
T[0]=v1,T[1]=v2,T[2]=v3;
returnOK;
}

字符串s中查找字符串t(c++源码)

编写函数intfind(chars[],chart[])在字符串s中查找字符串t,如果找到,则返回字符串t在字符串s中的位置。

#include<iostream.h>
#include<string.h>
intfind(char
s[],chart[]);
constintMAX=256;
intmain()
{
char
source[MAX],target[MAX];
cout<<"Pleaseinputastringfor
searching:/n";
cin.getline(source,MAX);
cout<<"Pleaseinput
astringyouwanttofind:/n";
cin.getline(target,MAX);
int
intPOs=find(source,target);
if(intPOs>=0)
{

cout<<"Findingit,Thetagertstringisatindex"<<intPOs<<"of
thesourcestring/n";
}
else
cout<<"Notfinding
it/n";
return0;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: