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; }
编写函数intfind(chars[],chart[])在字符串s中查找字符串t,如果找到,则返回字符串t在字符串s中的位置。
字符串s中查找字符串t(c++源码)
#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;
}
相关文章推荐
- 【整理】C语言中常见的字符串操作函数
- C语言实现字符串的操作
- C语言字符串操作总结大全(超详细)
- C语言常用字符串操作函数总结
- C语言--字符串基本操作
- C语言字符串操作(一)
- Java:布尔值,字符范围,字符串声明,换行操作,常量定义与c语言的不同
- 学习_c语言中对字符串的操作
- C语言的本质(22)——C标准库之字符串操作
- C语言字符串操作函数
- c语言字符串-常用操作(3)删除空格
- C语言中操作字符串的一些函数源代码
- C语言面试题之字符串操作
- 字符串操作(C语言 C++)
- C语言字符串操作总结大全(超详细)
- C语言自己编写的字符串操作的函数
- 常见的字符串操作(C语言实现)
- C语言字符串操作总结大全
- C语言字符串操作总结大全(超详细)
- fwrite fread fscanf fprintf 及C语言对字符串操作函数