首页 > 试题广场 >

封装一函数,实现统计指定位置上的字符出现在整个字符串中的次数

[问答题]
封装一函数,实现统计指定位置上的字符出现在整个字符串中的次数功能,比如:字符串“test_for_test”,指定第四位的‘t’为要查找的字符,运行后返回4(出现了4次)(任何语言都可以)
推荐
function countStr(str,s){
  return str.split(s).length-1;
}
countStr("test_for_test","t")
编辑于 2015-06-18 16:41:18 回复(1)
function getCount(str, index) {
  return str.split(str.substr(index - 1, 1)).length - 1; 
}

console.log(getCount('test_for_test', 4));
// 返回4

编辑于 2015-05-19 02:29:31 回复(0)

long count(string str,char target) {

    return count(str.begin(),str.end(),target);

}

发表于 2015-05-18 13:24:57 回复(0)
int 
count(char *string,int num)
{
    int count=0;
    char *temp=string;
    char ch=*(string+num);
    while(*temp!='\0')
    {
        if(*temp++==ch)
            count+=1;
    }
    return count;
}
发表于 2015-05-18 09:07:53 回复(0)
int fun(char str[],int n)
{
char ch=str[n-1];
int i=0,m=0;
while(str[i]!='\0')
{if(str[i++]==ch)
m++;}
return m;
}
发表于 2015-05-18 08:22:59 回复(0)
恩,这个题目还是偏向于简单的!只要大家有做过字符串的查找,我只知道C/C++使用的好像是strchr函数,java肯定使用String类的indexof方法,大家只要了解查找的步骤,首先是find然后是得到子字符串(不要忘记加上字符或字符串的长度),然后进行查找,次数用计数器使用
发表于 2015-05-18 07:10:41 回复(0)
static int getCharCount(String sourceStr,String target){
			int count = 0,last = 0;
			while( last != -1){
			sourceStr = sourceStr.substring( sourceStr.indexOf(target) +1 );
			last = sourceStr.indexOf(target);
			count++;
			}
			return count ;
		}

发表于 2015-09-19 05:27:00 回复(0)
//s为字符串;acter为要查找的字符
public int getNember(String s,String acter){
   char c[] = s.toCharArray();
   int count = 0;
   for(int i=0;i<c.length;i++){
     if(c[i] == acter){
      count += 1;
    }
   }
   return count;
}
编辑于 2015-05-19 14:55:29 回复(0)
int count(char *str,int i){
int ict=0;
for(int k=0;str[k]!='/0';k++)
if(str[k]==str[i])
ict++;
}
发表于 2015-05-19 11:44:00 回复(0)
import java.util.Scanner;

public class Tongji {
public static void main(String[] args) {
    Scanner input=new Scanner(System.in);
    System.out.print("请输入一个字符串:");
    String str=input.next();
    int count=0;
    System.out.print("请输入你要统计的字符:");
    String c=input.next();
    System.out.print(c+"在"+str+"中出现了");
    for (int i = 0; i < str.length(); i++) {
        if(str.indexOf(c)>=0){
            count++;
            str=str.substring(i+1);
        }
    }
    System.out.println(count+"次!");
}
}

发表于 2015-05-18 23:17:53 回复(0)
int(String str,char a){
  int count=0;
  a='t';
  str="test_for_test" ;
  for(int i=0;i<str.length();i++){
     if(str.charAt(i).equals(a)){
        count++;
     }
  }
return count;
}
发表于 2015-05-18 22:33:30 回复(0)
            string str = "test_for_test";
            char c = 't';
            int sum = 0;
            char[] chr = str.ToCharArray();
            foreach (char item in chr)
            {
                if (item == c)
                {
                    sum++;
                }
            }
            Console.WriteLine(sum);


发表于 2015-05-18 22:10:16 回复(0)
int count(int pos, char* str)
{
    if(str == NULL)
        return 0;
    if(pos > strlen(str)||pos<=0)
        return 0;
    char temp = *(str+pos-1);
    int curPos = 0;
    int countOfChar = 0;
    while(*(str+curPos) != '\0')
    {
        if (*(str+curPos) == temp)
        {
            countOfChar++;
        }
        curPos++;
    }

    return countOfChar;
}
编辑于 2015-05-18 22:03:29 回复(0)
int count_char(int pos , char *str)
{
    int count = 0 ;
    int i = 0    ;
    char deschar = str+pos ;
    for( ; str[i] != '\0' ; i++ )
    {
        if( str[i] == deschar ) count++ ;
    }
    return count ;
}

编辑于 2015-06-23 17:25:19 回复(0)
public static int countCharNumbers(String str, int index){
        if(str == null || index < 0)
            throw new IllegalArgumentException("输入的参数不合法!");
         
        char c = str.charAt(index);
        int count = 0;
         
        for(int i = 0; i < str.length();){
            int j = str.indexOf(c, i);
            if(j < 0){
                break;
            }else{
                count++;
                i = j + 1;
            }
        }
        return count;
    }
发表于 2015-05-18 21:38:19 回复(0)
function getCount(str,n){
  return str.split(new RegExp("["+str.charAt(n-1)+"]","gi")).length - 1;
}
console.log(getCount("test_for_test",4));

编辑于 2015-05-18 21:36:22 回复(0)
public static int countCharNumbers(String str, int index){
        if(str == null || index < 0)
            throw new IllegalArgumentException("输入的参数不合法!");
        
        char c = str.charAt(index);
        int count = 0;
        
        for(int i = 0; i < str.length();){
            int j = str.indexOf(c, i);
            if(j < 0){
                break;
            }else{
                count++;
                i = j + 1;
            }
        }
        return count;
    }


发表于 2015-05-18 20:32:41 回复(0)
C/C++
class solution{
public:
    int CountString(string a,char t){
        int count = 0;
        for(size_t i=0;i<a.size();++i){
            if(a[i]==t)
                count++;
      }
        return count;
}
python:
def count(a,t):
    count = 0
    for i in a:
        if i == t:
            count += 1
    return count

发表于 2015-05-18 20:22:39 回复(0)

int count( char *string, int n)

{

    n--;

    int totalCount = 0 ;

    for ( int i = 0 ; i < strlen (string); i++) {

        if (string[i] == string[n]) {

            totalCount++;

        }

    }

    return totalCount;

}

编辑于 2015-05-18 20:00:52 回复(0)
int count(char* str,char ch)
{
    int len=strlen(str);
    int i=0;
    while(i<len)
    {
        if(ch==*(str+i++))count++;
    }
return count;
}
发表于 2015-05-18 19:13:18 回复(0)
public static int charCount(int index,String str){
        
        if(index<0||index>=str.length()){
            throw new RuntimeException("超出字符串的小标范围");
        }
        char goal=str.charAt(index);
        System.out.println(goal);
        int i=0;
        int count=0;
        do{
            i=str.indexOf(goal, i);
            if(i<0){
                break;
            }else{
                count++;
            }
            
        }while(++i<str.length()&&i!=0);
        return count;
        
    }
发表于 2015-05-18 17:01:03 回复(0)