练习 4-1 编写函数 strindex(s, t),它返回字符串 t 在 s 中最右边出现的位置。 如果 s 中不包含 t,则返回-1
来源:互联网 发布:qq手机壁纸软件 编辑:程序博客网 时间:2024/05/21 20:24
#include <stdio.h>
#include <string.h>
#define MAXLINE 1000
int getlines(char line[],int max);
int strindex(char source[],char searchfor[]);
char pattern[] = "ould"; /*pattern to search for */
/*find all lines matching pattern */
int main(void)
{
char line[MAXLINE];
int k;
int found = 0;
while(getlines(line,MAXLINE) > 0){
if((k = strindex(line,pattern)) >= 0)
printf("%c",line[k]);
found++;
}
else
printf("%d\n",k);
}
return found;
}
int getlines(char s[],int lim)
{
int i,c;
i = 0;
while(--lim > 0 && (c = getchar()) != EOF && c != '\n')
s[i++] = c;
if(c == '\n')
s[i++] = c;
s[i] = '\0';
return i;
}
int strindex(char *s,char *t)
{
int i,j,k;
for(i = strlen(s) - 1; i >= 0; i--){
for(j = i,k = strlen(t)-1; k >= 0 && s[j] == t[k];k--,j--)
;
if(k < 0)
return (j+1);
}
return -1;
}
#include <string.h>
#define MAXLINE 1000
int getlines(char line[],int max);
int strindex(char source[],char searchfor[]);
char pattern[] = "ould"; /*pattern to search for */
/*find all lines matching pattern */
int main(void)
{
char line[MAXLINE];
int k;
int found = 0;
while(getlines(line,MAXLINE) > 0){
if((k = strindex(line,pattern)) >= 0)
{
printf("%s\n%d\n",line,k);
for(; line[k] != '\0';k++)printf("%c",line[k]);
found++;
}
else
printf("%d\n",k);
}
return found;
}
int getlines(char s[],int lim)
{
int i,c;
i = 0;
while(--lim > 0 && (c = getchar()) != EOF && c != '\n')
s[i++] = c;
if(c == '\n')
s[i++] = c;
s[i] = '\0';
return i;
}
int strindex(char *s,char *t)
{
int i,j,k;
for(i = strlen(s) - 1; i >= 0; i--){
for(j = i,k = strlen(t)-1; k >= 0 && s[j] == t[k];k--,j--)
;
if(k < 0)
return (j+1);
}
return -1;
}
阅读全文
0 0
- 4.1编写函数strindex(s,t)它返回字符串t在s中最右边出现的位置。如果s中不包含t,则返回-1。
- 4.1编写函数strindex(s,t)它返回字符串t在s中最右边出现的位置。如果s中不包含t,则返回-1。
- 练习 4-1 编写函数 strindex(s, t),它返回字符串 t 在 s 中最右边出现的位置。 如果 s 中不包含 t,则返回-1
- 练习4-1 编写函数strrindex(s,t),它返回字符串t在s中最右边出现的位置。如果s中不包含t,则返回-1
- 练习4-1 编写函数strrindex(s,t),它返回字符串t在s中最右边出现的位置。如果s中不包含t,则返回-1。
- 返回字符t在字符串s中最右边出现的位置,若s中不包括t,则返回-1
- 编写一个函数 char *strfind(char *s, char *t),用于查找字符串t在字符串s中最右边出现的位置,如果没有找到则返回NULL.
- 编写函数strrindex(s,t)它将返回字符串t在s中最右边出现的位置
- 全国计算机等级二级C++第四章习题编程题6 :编写一个函数 char *strfind(char *s, char *t),用于查找字符串t在字符串s中最右边出现的位置,如果没有找到则返回NULL.
- 练习5-4 编写函数strend(s,t)。如果字符串t出现在字符串s的尾部,该函数返回1;否则返回0
- 练5-4 习编写strend(s,t)函数。如果字符串t出现在字符串s的尾部,该函数返回1,否则返回0。
- 编写算法,求所有包含字符串S中而不包含字符串T中的字符串,构成的新串r以及r中每一个字符在S中第一次出现的位置
- 第4.1节 编写函数strindex(s,t)判定“该行包含指定的模式”
- 练习2-5 编写函数any(s1,s2),将字符串s2中的任一字符在字符串s1中第一次出现的位置作为结果返回。如果s1中不包含s2的字符,则返回-1。
- 练习 2-5 编写函数 any(s1, s2),将字符串 s2 中的任一字符在字符串 s1 中第一次 出现的位置作为结果返回。如果 s1 中不包含 s2 中的字符,则返回-1。
- 练习 2-5 编写函数 any(s1, s2),将字符串s2中的任一字符在字符串s1中第一次出现的位置作为结果返回。如果s1中不包含s2中的字符,则返回-1。
- 练习 2-5 编写函数 any(s1, s2),将字符串s2中的任一字符在字符串s1中第一次出现的位置作为结果返回。如果s1中不包含s2中的字符,则返回-1。
- 练习 2-5 编写函数 any(s1, s2),将字符串 s2 中的任一字符在字符串 s1 中第一次 出现的位置作为结果返回。如果 s1 中不包含 s2 中的字符,则返回-1
- ConcurrentHashMap1.7和1.8
- PHP中测试in_array、isset、array_key_exists性能
- 阿里cloud activemq
- 不容易系列之一
- c++的四种强制转换类型
- 练习 4-1 编写函数 strindex(s, t),它返回字符串 t 在 s 中最右边出现的位置。 如果 s 中不包含 t,则返回-1
- 016day(递归,库函数和头文件的学习)
- Constructing Roads
- 数组,集合的创建及赋值方式
- Java代码实现负载均衡五种算法
- Android Utils工具类集合
- 关于canvas画布使用fillRect()时高度会放大的解决方案
- 高数 01.02数列的极限
- 10月集训test13