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

حساب عدد الحروف الصغيره...

بدأه ruka في 22 نوفمبر 2010 · 15 رد · 1,129 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السؤال هـو

. Write a program to read a string and then count the number of small letters ('a'..'z') in that string.

كيف احسب جميع الحروف الصغيره

الحل اللي تحت لحساب حرفين فقط

 
import java.util.*;
public class sheet4_q2{
static Scanner console = new Scanner(System.in);
public static void main(String[] args){ 

int a= 0, z = 0;
String str;
char c;
System.out.println("Enter a sentence");

str = console.nextLine();

for ( int num =0 ; num<str.length(); num++)
{
 c =str.charAt(num);

if (a == 'a')  
a++ ;
else if ( z == 'z')
z++;
}
System.out.print("the number of a is" + a + "\n" );
System.out.print("the number of z is" + z);

}
}
#2
 int count[28];
//Zero the array
for(//for for the sentence)
{
c =str.charAt(num);
++count[c-'a'];
}
for(int i=0;i<28;i++)
{
print("count of " + (char)('a'+i) + "=" a);
}

تم تعديل هذه المشاركة بواسطة aohammed في 22 نوفمبر 2010 في 01:59

#3

هذا حل اخر

import java.util.*;

public class UnicodeTry
{
	static Scanner console = new Scanner(System.in);
	public static void main(String [] args)
	{
	 byte[] litrel;
	 byte tmp;
	 char[] small;
	 int num;
     String str;
     System.out.println("Enter a sentence"); 

     str = console.nextLine();
     litrel=new byte[str.length()];
     litrel=str.getBytes();
     small=new char[str.length()];

     for ( num =0 ; num<str.length(); ++num)
      {
	   tmp=litrel[num]	;
	   if(tmp>=97&&tmp<=122)
	    {
		 small[num]=str.charAt(num);
		 System.out.print(small[num]+" ");
	    }
      }
	}
}
1
albert_einstein.jpg

Untitled-AE.jpg
#4
aohammed كتب:

 int count[28];
//Zero the array
for(//for for the sentence)
{
c =str.charAt(num);
++count[c-'a'];
}
for(int i=0;i<28;i++)
{
print("count of " + (char)('a'+i) + "=" a);
}

معذره لدى سوال فى حلك

لماذا i<28

علما بان عدد الحروف 26 فقط

Software Developer
Mahmoudkelany.com


 

#5

هل هـذا الحل يعتبر صحيح

import java.util.*;
public class q2{
static Scanner console = new Scanner(System.in);
public static void main(String[] args){ 

int i = 0; 
int count;
char small;
String str;

System.out.println("Enter a sentence");
str = console.nextLine();

while (i<str.length())
{
small=str.charAt(i);
if (small>='a'&& small<='z')
++i;
System.out.print(i);





} 










}
}
#6
اقتباس
هل هـذا الحل يعتبر صحيح

؟

موالمطلوب حساب كل حرف كم عدد تكراره ؟ أم المقصود عدد الأحرف في الجملة :S ؟؟

اقتباس

معذره لدى سوال فى حلك

لماذا i<28

علما بان عدد الحروف 26 فقط

حقك علي يا أستاذي :( ذاكرتي بعدد الأحرف خانتني , شكراً لإلك

#7

المطلوب في السؤال حساب جميع الاحرف في الجمله small litter just

#8
aohammed كتب:

حقك علي يا أستاذي :( ذاكرتي بعدد الأحرف خانتني , شكراً لإلك

لا طبعا ما فى شىء

انا عارف انك بتعرف ذلك

وانت مو صغير فالبرمجه ربنا يوفقك يارب

لكن كنت اتسال هل هذا خطأ ام هناك فائده من ذلك

Software Developer
Mahmoudkelany.com


 

#9
import java.util.Scanner;

public class q2 {
	static Scanner console = new Scanner(System.in);

	public static void main(String[] args){ 

		int count = 0;
		String str;

		System.out.println("Enter a sentence");
		str = console.nextLine();

		for  (int i = 0; i < str.length(); i++) {
			char c = str.charAt(i);
			if (Character.isLowerCase(c)) count++;
			//or
			//if (c >= 'a' && c <= 'z') count++;
		}
		System.out.println("The number of small letters is: "+count);
	}
}
2
#10
ruka كتب:

هل هـذا الحل يعتبر صحيح

import java.util.*;
public class q2{
static Scanner console = new Scanner(System.in);
public static void main(String[] args){ 

int i = 0; 
int count;
char small;
String str;

System.out.println("Enter a sentence");
str = console.nextLine();

while (i<str.length())
{
small=str.charAt(i);
if (small>='a'&& small<='z')
++i;
System.out.print(i);





} 










}
}

جرب لتعرف صحيح أم لا!!

#11

جربت طريقة للاسف شغالة بس مش و لا بد قوى

لكن حل الأخ herch أفضل طريقة

#12
mr.beshoy كتب:

جربت طريقة للاسف شغالة بس مش و لا بد قوى

لكن حل الأخ herch أفضل طريقة

لم افهم عليك ما المشكله

albert_einstein.jpg

Untitled-AE.jpg
#13

بقول كنت كتبت طريقة اشتغلت لكن مش بكفأة كويسة كان في غلطة منطقية مش أكتر.

لكن الطريقة الى عملها الأخ Herch واضحة و كويسة

و قرأت طريقتك بردو أعجبتنى و استفدت منهم .

------------------------------------------------------

#14
herch كتب:

			Character.isLowerCase(c)

+2


﴿وَمَا كَانَ اللَّهُ لِيُعَذِّبَهُمْ وَأَنْتَ فِيهِمْ ۚ وَمَا كَانَ اللَّهُ مُعَذِّبَهُمْ وَهُمْ يَسْتَغْفِرُونَ﴾

الأنفال ۳۳                                  
 

#15

شكراً لك اخي herch اجابة نموذجيه.. ولجميع من ابدى حلوله استفدت من الطرق المختلفه للحل :wub:

مشكلت الكود اللي عملته انه لم يكن قوي..

اذا امكن ان اعرض كود اخر عملته لسؤال مختلف

المشكلة اني مااعرفت السؤال ماهو طلبه

والكـود اجتهاد مني فهو صحيح لكن يوجد شي ناقص ولم اعرف مااهو...T T

Write a program that reads a positive integer number and then

Calculates and displays the factorial of that number, also display if the number is even or odd .

import java.util.*;
public class sheet4_q1{
static Scanner console = new Scanner(System.in);
public static void main(String[]args){

int posnum; //positive number
int num;

System.out.print("Enter a number");
posnum = console.nextInt();

for (int i=0; i<posnum; i++);
{
num = console.nextInt();

if (num %2 == 0)
System.out.print("the number is even");

else if (num %2 == 1)
System.out.print("the number is odd");




}





}


}
#16

هذا حل لي سؤالك الثاني

يريد السؤال factorial number

مثال

6=1*2*3=!3

لقد اضفت (do wile loop)لتفادي الارقام السالبه اذا لم تصل الى مرحله الloops في تعليمك فحذفها واضف if statement

ولم اسخدم ال(Recursion Method)لاني اعتقد انك لم تصل في تعليمك اليها


public class sheet4_q1
{
static Scanner console = new Scanner(System.in);
public static void main(String[]args)
{

int posnum=0; //positive number
int num;
System.out.println("Enter a number");
do
{
if(posnum>0)
{
System.out.println("Error: negtive number");
System.out.println("please Enter a number again");
}
num=console.nextInt();
++posnum;
}while(num<0);

if (num %2 == 0)
System.out.println("the number is even");

if(num %2 == 1)
System.out.println("the number is odd");

int product=1;
System.out.print("the factorial of "+num+"is ");
for(int i=num;i>0;--i)
{
product*=i;
}
System.out.print(product);


}

}
import java.util.*;

وشكرا

تم تعديل هذه المشاركة بواسطة pro.fahd في 24 نوفمبر 2010 في 01:32

albert_einstein.jpg

Untitled-AE.jpg

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