program that counts the characters input from the sentence.

Discussion in 'C++' started by rayjhen12, Mar 19, 2011.

  1. rayjhen12

    rayjhen12 New Member

    Joined:
    Mar 19, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    can somebody give me the code for the program that "allows the user to enter a sentence" then the program will count and sort the characters input. I need this on 2 days so please, somebodyh help me, please.
     
  2. jsh

    jsh New Member

    Joined:
    Mar 18, 2011
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    JSH:Hi...

    Please note....
    ----------
    Code:
    /* Javad Sharyati */
    
    #include<iostream.h>
    #include<conio.h>
    
    char str[100];
    int leng=-1;
    
    //-------------------
    
    void get_str()
    {
    cout<<"\n\n Please Enter a Sentence \n ------ \n";
    char ch=getch();
    int i=0;
    while(ch != '\r' )
    {
      int chr=(int) ch ;
      if( chr == 8 &&  i > 0 )
         {
            str[i]='\0';
            leng--;
             i--;
         }
      if((chr>=48 && chr<=57)||(chr>=65 && chr<=90)||(chr>=97 && chr<=122)||chr==32)
        {
           str[i]=ch;
           leng=i;
           i++;
        }
     }
    }
    
    //------------------------
    
    void swap(int a,int b)
    {
      char temp;
      temp=str[a];
      str[a]=str[b];
      str[b]=temp; 
    }
    //------------------------
    void sort()
    {
      for(int i=0;i<leng;i++)
       {
          for(int j=i+1;j<=leng;j++)
             {
                 if(str[i] < str[j])
                   swap(i,j);
              }
       }
    }
    
    //------------------
    void out()
    {
      cout<<"\n-------------\n ";
      for(int i=0;i<=leng;i++)
       cout<<str[i];
    }
    
    //-------------------
    void main()
    {
      get_str();
      sort();
      out();
    }
    
    
    you can chang functions( sort , get_str , swap , out );
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice