10-1. 在字符串中查找指定字符(15)

输入一个字符串S,再输入一个字符c,要求在字符串S中查找字符c。如果找不到则输出“Not found”;若找到则输出字符串S中从c开始的所有字符。

输入格式:

输入在第1行中给出一个不超过80个字符长度的、以回车结束的非空字符串;在第2行中给出一个字符。

输出格式:

在一行中按照题目要求输出结果。

输入样例1:

It is a black box
b

输出样例1:

black box

输入样例2:

It is a black box
B

输出样例2:

Not found
 1 #include <stdio.h>
 2 #include <string.h>
 3
 4 int main()
 5 {
 6     char str[81];                               //输入的字符串
 7     char ch;                                    //要查找的字符
 8     int i;
 9     for(i = 0; i < 81; i++) {                   //输入字符串,遇 ‘\n‘ 时改成  ‘\0‘ 并结束
10         scanf("%c", &str[i]);
11         if(str[i] == ‘\n‘) {
12             str[i] = ‘\0‘;
13             break;
14         }
15     }
16
17     scanf("%c", &ch);                            //输入要查找的字符
18     int t, flag = 0;
19     for(i = 0; i < strlen(str); i++) {           //从字符串第一个字符到最后一个字符(不是‘\0‘)循环查找
20         if(str[i] == ch) {                       //如果找到测用 t 记录下标,并使 flag = 1切结束循环
21             t = i;
22             flag = 1;
23             break;
24         }
25     }
26     if(flag == 1) {
27         for(i = t; i < strlen(str); i++) {       //从下标 为 t 的字符到最后一个字符依次输出
28             printf("%c", str[i]);
29         }
30     }
31     else {
32         printf("Not found");                     //没有找到
33     }
34     return 0;
35 }

10-1. 在字符串中查找指定字符(15)

时间: 2024-08-01 22:55:12

10-1. 在字符串中查找指定字符(15)的相关文章

字符串-01. 在字符串中查找指定字符(15)

输入一个字符串S,再输入一个字符c,要求在字符串S中查找字符c.如果找不到则输出“Not found”:若找到则输出字符串S中从c开始的所有字符. 输入格式: 输入在第1行中给出一个不超过80个字符长度的.以回车结束的非空字符串:在第2行中给出一个字符. 输出格式: 在一行中按照题目要求输出结果. 输入样例1: It is a black box b 输出样例1: black box 输入样例2: It is a black box B 输出样例2: Not found 1 #include <

*字符串-01. 在字符串中查找指定字符

1 /* 2 * Main.c 3 * D1-字符串-01. 在字符串中查找指定字符 4 * Created on: 2014年8月18日 5 * Author: Boomkeeper 6 *****部分通过****** 7 */ 8 9 #include <stdio.h> 10 11 int mysearch(char ch, const char str[], int length) { 12 13 int j, ret = -1; 14 15 for (j = 0; j < le

在字符串中查找指定字符

输入一个字符串S,再输入一个字符c,要求在字符串S中查找字符c.如果找不到则输出“Not found”:若找到则输出字符串S中从c开始的所有字符. 输入格式: 输入在第1行中给出一个不超过80个字符长度的.以回车结束的非空字符串:在第2行中给出一个字符. 输出格式: 在一行中按照题目要求输出结果. 输入样例1: It is a black box b 输出样例1: black box 输入样例2: It is a black box B 输出样例2: Not found #include<std

【C语言】模拟实现strchr函数.即在一个字符串中查找一个字符第一次出现的位置并返回

//模拟实现strchr函数.即在一个字符串中查找一个字符第一次出现的位置并返回 #include <stdio.h> //#include <string.h> #include <assert.h> char* my_strchr(char *dst, char src) { assert(dst); while (*dst != '\0') { if (*dst == src) return dst; dst++; } return 0; } int main()

【c语言】模拟实现strchr函数,功能:在一个字符串中查找一个字符第一次出现的位置,如果没有出现返回NULL

// 模拟实现strchr函数,功能:在一个字符串中查找一个字符第一次出现的位置,如果没有出现返回NULL #include <stdio.h> #include <assert.h> char const* my_strchr(char const *p,char c) { assert(p != NULL); while (*p) { if (*p == c) return p; else p++; } return NULL; } int main() { char *p =

第3章-4 查找指定字符 (15分)

本题要求编写程序,从给定字符串中查找某指定的字符. 输入格式: 输入的第一行是一个待查找的字符.第二行是一个以回车结束的非空字符串(不超过80个字符). 输出格式: 如果找到,在一行内按照格式“index = 下标”输出该字符在字符串中所对应的最大下标(下标从0开始):否则输出"Not Found". 输入样例1: m programming 输出样例1: index = 7 输入样例2: a 1234 输出样例2: Not Found 1 # 查找指定字符 2 # Author: c

python 提取字符串中的指定字符 正则表达式

例1: 字符串: '湖南省长沙市岳麓区麓山南路麓山门' 提取:湖南,长沙 在不用正则表达式的情况下: address = '湖南省长沙市岳麓区麓山南路麓山门' address1 = address.split('省') # 用“省”字划分字符串,返回一个列表 address2 = address1[1].split('市') # 用“市”字划分address1列表的第二个元素,返回一个列表 print(address1) # 输出 ['湖南', '长沙市岳麓区麓山南路麓山门'] print(ad

oracle去掉字符串中所有指定字符

Select Replace(字段名,'指定字符','替换字符') From 表名 --例: select replace('de.5d','.','') from dual --显示结果:de5d 转:https://blog.csdn.net/myflysun/article/details/26621731 原文地址:https://www.cnblogs.com/wangfuyou/p/10340181.html

c 删除字符串中的指定字符

#include <stdio.h> #include <string.h> void delChar(char *s, char ch) { int i,j; int len = strlen(s); for(i = 0; i < len; i++) { if(s[i] == ch) { for(j = i; j < len; j++) { s[j] = s[j+1]; i--; } } } } int main(int argc, char *argv[]) { p