字符串中查找字母有几个

p = 0
s = ‘How can i get there?‘
for i in s:

    if i == ‘P‘or i == ‘p‘:
        p = p + 1
    else:
        continue

if p != 0:
    print(‘有‘,p,‘个‘)
else:
    print(‘无‘)
    
时间: 2024-07-30 06:33:43

字符串中查找字母有几个的相关文章

算法积累(字符串转换驼峰,判断一个字符串中那个字母出现次数最多,并且出现了几次)

因为算法比较烂,所以想做一下这方面的积累. 尽量能够每天学习一个新算法吧.(不过估计很悬) 好吧,今天第一个是字符串转换驼峰 直接上代码 var str = 'toupper-case'; var arr = str.split('-'); //toupper,case for (var i = 1; i < arr.length; i++) { //把除了第一个数组后面的数组的第一个值设置为大写然后大写字母和去掉第一个字符的剩下的字符进行拼合 arr[i] = arr[i].charAt(0)

*字符串-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

华为机试—替换字符串中的字母

功能描述:将字符串中的字母全部替换成字母的下一个字母,要是最后一位是z或Z则替换为a或A. *        输入:aBxyZ *        输出:bCyzA #include<iostream> #include<string> #include<cctype> using namespace std; char ml[]="abcdefghijklmnopqrstuvwxyza"; char mu[]="ABCDEFGHIJKLMN

345. 反转字符串中元音字母的位置 Reverse Vowels of a String

Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Given s = "hello", return "holle". Example 2:Given s = "leetcode", return "leotcede" 题意:反转字符串中元音字母的位置 方法1:用栈保存元音字符串,时间

字符串中查找子串

使用C语言编写程序: 1.在字符串中查找一个指定的字符第一次出现的位置,并返回字符所在的位置,如果不存在则返回NULL 具体实现如下: char* strchr(char const *str, int ch) { char* st = (char*)str; while (st) { if (*st == ch) return st; st++; } return NULL; } 2.在字符串中查找一个指定的字符串第一次出现的位置,并返回字符所在的位置,如果不存在则返回NULL 具体实现如下:

在字符串中查找指定字符

输入一个字符串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

模拟实现在一个字符串中查找一个字符串

在标准库中有一个函数strstr()用于在一个字符串中查找一个规定的字符串,这个函数可以模拟实现一下,代码如下: #include <stdio.h> #include <assert.h> char *my_strstr(const char str[],const char strstr[]) {  int i = 0,j = 0,k = 0;  assert(str != NULL);  assert(strstr != NULL);  for(i = 0;str[i] !=

【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()

在父字符串中查找子字符串

在父字符串中查找子字符串(指针控制,也可选择标控制) #pragma once #include<iostream> #include<assert.h> using namespace std; char* StrStr(char* source, char* dest) { assert(source&&dest); if (strlen(source) < strlen(dest)) return NULL; char* newSrc = NULL; c