指针的简单应用

题目:http://acm.swust.edu.cn/oj/problem/448/

在本题中我实现了用指针读入字符串的功能。

并通过指针的操作来实现本题的功能。

View Code
#include<stdio.h>

#include<stdlib.h>

#include<string.h>



int main(){

    char *str, *p;

    char *sen, *sp;

    char ch;

    p = (char *)malloc(sizeof(char));

    str = p;

    while(scanf("%c", &ch)!=EOF){

        if(ch=='\n') break;

        *p = ch;

        p++;

    }

    sp = (char*)malloc(sizeof(char));

    sen = sp;

    while(scanf("%c", &ch)!=EOF){

        if(ch=='\n') break;

        *sp = ch;

        sp++;

    }

    int i, j, Lens = strlen(sen), Lenw = strlen(str), pos;

    p = str;

    sp = sen;

    char *ssp;

    for(i=0; i<Lens; i++){

        if(*sp==*p){

            ssp = sp;

            pos = i+1;

            for(j=1; j<Lenw; j++){

                sp++;

                p++;

                if(*sp!=*p) break;

            }

            if(j==Lenw-1){

                break;

            }else{

                sp = ssp;

                p = str;

            }

        }

        sp++;

    }

    printf("%d\n", pos);

}

你可能感兴趣的:(指针)