C语言字符串操作

插入法排序(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>
int main()
{
int a[10],r[11];
int *p;
int i,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);
}
return 0;
}

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

 

#include <iostream.h>
#include <string.h>
int check(char *s)
{
    char *p1;
    char *p2;
    int n;
    n=strlen(s);
    p1=s;
    p2=s+n-1;
    while (p1<p2)
    {
        if (*p1++!=*p2--)
        {
            return 0;
        }
        else
         return 1;
    }
}
int main()
{
    char str[100];
    cout<<"Input your string:";
    cin>>str;
    if (check(str))
    {
        cout<<"This is huiwen!"<<endl;
    }
    else
        cout<<"This not huiwen!"<<endl;
    return 0;
}

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

 

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

#include <stdlib.h>
#include <stdio.h>
typedef int Status;
typedef int ElemType;
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef struct 
{
    ElemType *elem;
    int length;
    int listsize;
}Sqlist;
Status InitList_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;
    return OK;
}
Status scan(Sqlist &L,int i)
{
    for(i=0;i<L.length;i++)
    {
        printf("%d/t",L.elem[i]);
    }
    return OK;   
}

(

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

 

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

#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef int ElemType;
typedef ElemType *Triplet;
Status InitTriplet(Triplet &T)
{   
    ElemType v1, 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;
    return OK;
}  
 

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

 

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

#include <iostream.h>
#include <string.h>
int find(char s[],char t[]);
const int MAX=256;
int main()
{
    char source[MAX],target[MAX];
    cout<<"Please input a string for searching:/n";
    cin.getline(source,MAX);
    cout<<"Please input a string you want to find:/n";
    cin.getline(target,MAX);
    int intPOs=find(source,target);
    if (intPOs>=0)
    {
        cout<<"Finding it,The tagert string is at index"<<intPOs<<"of the source string/n";
    }
    else
        cout<<"Not finding it/n";
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值