الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

كيف أحسب عدد مرات وجود String داخل آخر

بدأه محمد صلاح عبدالحميد في 4 مايو 2008 · 2 رد · 483 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

فكرة الكود أنه يطلب من المستخدم أن يدخل 2 strings ثم بعد ذلك يحسب عدد مرات حدوث الأول داخل الثاني أنا حاولت في هذ1ا الكود لكن لا يعمل بشكل جيد أرجو المساعدة

public class Main {
 static Scanner  console = new Scanner(System.in);
	public static void main(String[] args)
	{
		System.out.println("Enter Two stings ");
		System.out.println();
		String str1 = console.nextLine();
		String str2 = console.nextLine();
	   print (count (str1,str2 ));  
	}	   
	public static int  count (String x, String y)
	{
	int num = x.replaceAll(x ,y).length();
	return(num);
	}		
	public static void  print (int b)
	{
	System.out.println("The Number of occrrunces is " +b);
	}
}

تم تعديل هذه المشاركة بواسطة محمد صلاح عبدالحميد في 4 مايو 2008 في 13:30

#2
import java.util.Scanner;
 class Main {
static Scanner  console = new Scanner(System.in);
	public static void main(String[] args)
	{
		System.out.println("Enter Two stings ");
		System.out.println();
		String str1 = console.nextLine();
		String str2 = console.nextLine();
	   print(count(str1,str2 ));  
	}	  
	public static int  count (String str1, String str2)
	{
		int count=0;
  while(str2.contains(str1)){
			count++;
			int index=str2.indexOf(str1);  
  str2=str2.substring(index+str1.length());


  }
		return count;
	}		
	public static void  print (int b)
	{
	System.out.println("The Number of occrrunces is " +b);
	}
}
#3

جزاك الله خيرا على مجهودك

مواضيع مشابهة