#include<iostream.h>


/*********************************************/
//مجموعة من الوال والعمليات على النص والحروف**/
// كتبه السهم الناري**************************/
/*********************************************/


class STRING
{
public:
	STRING (){}	
	~STRING (){}


	int strlen( char  *ptr )const;
	int  search( char const  *ptr)const;
	char * strupr( char   *ptr  );
	char * strlwer( char  *ptr  );
	char * SmallORLarge_char( char  *ptr  );
	char * OneSpace( char *ptr  );
	char * strcpy( char *ptr  );
	char * strrev( char *ptr  );
	char * Replace( char *ptr );
	char * strset(char *ptr ,char ch);
	char * strnset(char *ptr ,char ch,int length);
	char * strcat(char  *ptr1 ,  char  *ptr2 );
	int CounterLarge(char const *ptr )const;
	int CounterSmall(char const *ptr )const;
	int  CounterNumber(char const *ptr )const;
	int CounterSpace(char const *ptr )const;
	int CounterCarcter(char const *ptr )const;
	void  ReadData(char  *ptr );	
	void Menu();

private :
	char str[80];
	
	

};


//دالة تقرأ النص المرر لها
void  STRING::ReadData(char *ptr)
{
	

	cout << "\n Enter String : ";
	cin.ignore(1,'\n');
	cin.getline(ptr,80);
}


//دالة لحساب عدد الحروف الكبيرة
int STRING::CounterLarge(char const *ptr) const
{
	int large = 0;	

	while( *ptr!='\0' )
	{
		if(*ptr>=65 && *ptr<=90) //عدد الحروف الكبيرة
			large++;
		ptr++;
	}
	 

	return large;
}

//دالة لحساب عدد الحروف الصغيرة
int STRING::CounterSmall(char const *ptr) const
{
	int small = 0;	

	while( *ptr!='\0' )
	{
		if(*ptr>=97 && *ptr<=122) //عدد الحروف الصغيرة
			small++;
		ptr++;
	}

	return small;
}

//دالة لحساب عدد الأرقام
int STRING::CounterNumber(char const *ptr) const
{
	int number = 0;	

	while( *ptr!='\0' )
	{
		if(*ptr>=48 && *ptr<=57) //عدد الأرقام
			number++;
		ptr++;
	}

	return number;
}

//دالة حساب عدد الفراغات
int STRING::CounterSpace(char const *ptr) const
{
	int space = 0;	

	while( *ptr!='\0' )
	{
		if(*ptr==32) //عدد الفراغات
			space++;
		ptr++;
	}

	return space;
}

//دالة حساب عدد الرموز الخاصة
int STRING::CounterCarcter(char const *ptr) const
{
	int carcter = 0;	

	while( *ptr!='\0' )
	{
		if( (*ptr>=33 && *ptr<=47) || (*ptr>=58 && *ptr<=64) || (*ptr>=91 && *ptr<=96) || (*ptr>=123 && *ptr<=127) )
			carcter++;
		ptr++;
	}

	return carcter;
}

//دالة تضيف نص إلى نص
//دالة strct
char * STRING::strcat(char  *ptr1 ,  char  *ptr2 )
{
	char * str_aux = ptr1;
	char * str_ptr = ptr2;
	int len;
	int len_src;
	int i;

	len = strlen(str_aux) + strlen(str_ptr);
	ptr1 = new char[len];
	ptr1[len] = '\0';

	len = strlen(str_aux);
	for (i = 0; i < len ; i++)
	{
		ptr1[i] = str_aux[i];
	}

	len_src = strlen(str_ptr);
	for (i = 0; i < len_src ; i++)
	{
		ptr1[len + i] = str_ptr[i];
	}

	return  strcpy(ptr1);

}




//دالة تبدل حرف معين بكامل حروف النص
char * STRING::strset(char *ptr,char ch)
{
	char *ptr1 = ptr;
	
	while( *ptr1!='\0' )
	{
		*ptr1 = ch;
		ptr1++;
	}

	return  strcpy(ptr);
}

//دالة تبدل حرف معين بعدد معين من حروف النص
char * STRING::strnset(char *ptr,char ch,int length)
{
	char *ptr1 = ptr;	
	int i=0;

	while( *ptr1!='\0' )
	{
		if(i<length)		
			*ptr1 = ch;					
			i++;
		ptr1++;
	}

	return  strcpy(ptr);
}

//دالة تبدل الحرف الذي لاتريد بالحرف الذي تريد
char *STRING::Replace( char *ptr)
{
	char ch1,ch2;
	char *ptr1 = ptr;

	cout <<"\nEnter Char : ";
    cin >> ch1;	

	while( *ptr1!='\0' )
	{
		if( *ptr1==ch1 )
		{	
			cout <<"\n Enter Replace  Char : ";
			cin >> ch2;		
			*ptr1 = ch2;
           
		}
		else
		{
			ptr1++;
	    
		}
	}
			

		return  strcpy(ptr);
}
	

//دالة تبحث عن  كم مرة تكرر الحرف في السلسلة
int STRING::search( char const *ptr) const
{
	
	char ch;
	int i=0;

	cout <<"\nEnter One Carcter : ";
	cin >> ch;

	while(*ptr!='\0')
	{
		if( *ptr==ch )
		{
			  i++;
		      ptr++;
		}
		else
		{
			ptr++;
		}
	}

	return i;
}

//دالة لحساب عدد حروف مصفوفة
int STRING::strlen(char   *ptr) const 
{
		int  counter=0;		
		
		while( *ptr!='\0' )
		{
			
			counter++;
			ptr++;
		}
		*ptr='\0';
		counter++;

		return  counter;
}



//دالة لتحويل من صغير إلى كبير بالنسبة لسلسلة حرفية

char * STRING::strupr(char *ptr)
{
	char *ptr1 = ptr;
	
		while( *ptr1!='\0' )
		{
			if( *ptr1>='a' && *ptr1<='z' )		
				*ptr1 = *ptr1 + ('A'-'a');				
				ptr1++;	
		
		}    
	
		return  strcpy(ptr);	
}

//دالة لتحويل الحرف من كبير إلى صغير
char * STRING::strlwer(char  *ptr)
{
	char *ptr1 = ptr;

		while( *ptr1!='\0' )
		{
			if( *ptr1>='A' && *ptr1<='Z' )
				*ptr1 = *ptr1 - ('A'-'a');
				ptr1++;
		}

		return  strcpy(ptr);
		
}

 //دالة لتحويل من صغير إلى كبير أو العكس من كبير إلى صغير
char * STRING::SmallORLarge_char(char  *ptr)
{
	char *ptr1 = ptr;		

		while( *ptr1!='\0' )
		{
			if(*ptr1>='A' && *ptr1<='Z')
			{
				*ptr1 = *ptr1 - ('A'-'a');
				
			}
		
		   else if(*ptr1>='a' && *ptr1<='z')
		   {
				*ptr1 = *ptr1 + ('A'-'a');
			 
		   }

			ptr1++;
		}

		return   strcpy(ptr);
}

//دالة لجعل بين الحروف فراغ واحد فقط
char *  STRING::OneSpace(char *ptr)
{
		char *ptr1,*ptr2;
		int flag=0;	

		ptr1=&ptr[0];
		ptr2=&str[0];

		while ( *ptr1 !='\0' )
		{
			if(*ptr1!=' ')
			{
				*ptr2=*ptr1;
				ptr1++;
				ptr2++;
				flag=0;
			}
			else
			{
				if(flag==0)
				{
					*ptr2=*ptr1;
					ptr1++;
					ptr2++;
					flag=1;
				}
				else
				{
					ptr1++;
				}
			}
		}

		*ptr2='\0';

	  return  str;
		

}

	//دالة لنسخ محتويات نص ووضعها بسلسلة أخرى
char * STRING::strcpy(char *ptr)
{
		char *ptr1,*ptr2;		
	
		
		ptr1=&ptr[0];
		ptr2=&str[0];

		while ( *ptr1 !='\0' )
		{	
				*ptr2=*ptr1;
				ptr1++;
				ptr2++;			
		}

		*ptr2='\0';

		return  str;
	
		
}

//دالة المعكوس

char *STRING::strrev(char *ptr)
{
	
   	int	 n = strlen(ptr);
	char *ptr2,temp,*ptr1;

	ptr1=&ptr[0];
	ptr2=&ptr[n-1];  

	while( ptr1<=ptr2 )
	{
		temp  = *ptr1;
		*ptr1 = *ptr2;
		*ptr2 = temp;

		ptr1++;
		ptr2--;
	}



	return  strcpy(ptr);   //تعود بمؤشر ناتج المعكوس
	
}

void STRING::Menu()
{
	char  string[80],ch,sh;
	char  str[80];
	char  *pstr1,*pstr2;
	int chose,len;

	pstr1 = string;
	pstr2 = str;
	

	while( ch!='n' )
	{
    cout <<"\n*********************MENU Function String*********************\n";
	cout <<"\n (1)Function Strlen ";
	cout <<"\n (2)Function Strlwer ";
	cout <<"\n (3)Function Strupr ";
	cout <<"\n (4)Function SmallORLArge Carcter ";
	cout <<"\n (5)Function Strrev ";
	cout <<"\n (6)Function One Space Carcter ";
	cout <<"\n (7)Function Search ";
	cout <<"\n (8)Function Replace ";
	cout <<"\n (9)Function Strset ";
	cout <<"\n (10)Function Strnset ";
	cout <<"\n (11)Function Strcat ";
	cout <<"\n (12)Function Compute is Large Carcter [CounterLarge] ";
	cout <<"\n (13)Function Compute is Small Carcter [CounterSmall] ";
	cout <<"\n (14)Function Compute is Number Carcter[CounterNumber] ";
	cout <<"\n (15)Function Compute is Space Carcter [CounterSpace] ";
	cout <<"\n (16)Function Compute is char Carcter  [CounterCarcter] ";
	cout <<"\n****************************************************\n";
	cout <<"\n Chose = ";
	cin >> chose;

	switch(chose)
	{
	
	case 1:
		{
				ReadData(pstr1);
			    cout <<"\n length is  = " << strlen(pstr1) <<"\n";
				break;
		}
	case 2:
		{
				ReadData(pstr1);
				cout <<"\n Strlwer = " << strlwer(pstr1) <<"\n";
				break;
		}
	case 3:
		{
				ReadData(pstr1);
				cout <<"\n Strupr = " << strupr(pstr1) <<"\n";
				break;
		}
	case 4:
		{
				ReadData(pstr1);
				cout <<"\n Small Or Large Carcter = " << SmallORLarge_char(pstr1) <<"\n";
				break;
		}
	case 5:
		{
				ReadData(pstr1);
				cout <<"\n Strrev = " << strrev(pstr1) <<"\n";
				break;
		}

	case 6:
		{
				ReadData(pstr1);
				cout <<"\n One speac = " << OneSpace(pstr1) <<"\n";
				break;
		}
	case 7:
		{
				ReadData(pstr1);
				cout <<"\n Counter = " << search(pstr1) <<"\n";
				break;
		}
	case 8:
		{ 
				ReadData(pstr1);
				cout <<"\n Replace = " << Replace(pstr1) <<"\n";
				break;
		}
	case 9:
		{ 
				ReadData(pstr1);
				cout << "\n Enter One char : ";
				cin  >> sh;
				cout <<"\n strset = " << strset(pstr1,sh) <<"\n";
				break;
		}
	case 10:
		{ 
				ReadData(pstr1);
				cout << "\n Enter One char : ";
				cin  >> sh;
				cout << "\n Enter Length of pstr1 : ";
				cin  >> len; 
				cout <<"\n strset = " << strnset(pstr1,sh,len) <<"\n";
				break;
		}
	case 11:
		{ 
				ReadData(pstr1);
				ReadData(pstr2);			
				cout <<"\n strcat = " << strcat(pstr1,pstr2) <<"\n";
				break;
		}
	case 12:
		{ 
				ReadData(pstr1);
				cout <<"\n CounterLarge = " << CounterLarge(pstr1) <<"\n";
				break;
		}
   case 13:
		{ 
				ReadData(pstr1);
				cout <<"\n CounterSmall = " << CounterSmall(pstr1) <<"\n";
				break;
		}
  case 14:
		{
				ReadData(pstr1);
				cout <<"\n CounterNumber = " << CounterNumber(pstr1) <<"\n";
				break;
		}
  case 15:
		{
				ReadData(pstr1);
			    cout <<"\n CounterSpace = " << CounterSpace(pstr1) <<"\n";
				break;
		}
		
 case 16:
		{
				ReadData(pstr1);
				cout <<"\n CounterCarcter = " << CounterCarcter(pstr1) <<"\n";
				break;
		}

			
	default :
		{
			cout <<"\a\a\a error chose Enter 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 \n";
		}
	}

     cout <<"\n Your are Continue(y/n) : ";
	 cin >> ch;
	  
	}


}
int main()
{
	STRING S;

	S.Menu();

    return 0;
}
	