指針的簡單應用
題目: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); }
posted on 2012-05-06 19:56 More study needed. 閱讀(198) 評論(0) 收藏 舉報

浙公網安備 33010602011771號