Chinaunix首页 | 论坛 | 博客

AAA

  • 博客访问: 18394
  • 博文数量: 33
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 330
  • 用 户 组: 普通用户
  • 注册时间: 2015-04-17 08:23
个人简介

好记性不如烂笔头,记录学习历程和随想云云,欢迎各位大虾拍砖

文章分类

全部博文(33)

文章存档

2015年(33)

我的朋友
最近访客

分类: C/C++

2015-05-07 23:20:17

【问题描述】

Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.


【解决方案】

点击(此处)折叠或打开

  1. int strStr(char* haystack, char* needle) {
  2.     if(NULL == needle)
  3.     {
  4.         return 0;
  5.     }
  6.     
  7.     int i;
  8.     int haystackLen = strlen(haystack);
  9.     int needleLen = strlen(needle);
  10.     
  11.     if(haystackLen < needleLen)
  12.     {
  13.         return -1;
  14.     }
  15.     
  16.     for(i=0; i<=(haystackLen - needleLen); i++)
  17.     {
  18.         if(0 == strncmp(haystack+i, needle, needleLen))
  19.         {
  20.             return i;
  21.         }
  22.     }
  23.     
  24.     return -1;
  25. }

阅读(278) | 评论(0) | 转发(0) |
0

上一篇:String to Integer (atoi)

下一篇:Number of 1 Bits

给主人留下些什么吧!~~