string manipulation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • apsonline
    New Member
    • Aug 2006
    • 10

    string manipulation

    hi friends
    m tryng to write a program that:

    1)takes the string and returns the strings in alphabetical order,including the no. of times they appera in the string.

    eg:

    input:dolly
    output:d1l2o1y1

    2)related with the above is the other program that:

    "compresses " the given string
    eg
    input:aaaabbbbc ccdddd
    output:a4b4c3d4

    can u guys help me out?
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Have an attempt yourself first, then we will help you debug it.

    Comment

    • rgb
      New Member
      • Aug 2006
      • 37

      #3
      you should use a loop to manipulate your input word. if you increment a "char" type in c/c++ you will get the next letter in the alphabet, you can use that as a templete to check the count of each letter in your input word.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Originally posted by rgb
        you should use a loop to manipulate your input word. if you increment a "char" type in c/c++ you will get the next letter in the alphabet, you can use that as a templete to check the count of each letter in your input word.
        Strictly speaking not quite true.

        C/C++does not require any particular character set and only requires that char incrementation produces the next char in the series for the digits 0 1 2 3 4 5 6 7 8 9. If does not require it for the letters. The fact that incrementing a char gives the next letter is actually a property of the character set (ASCII most likely) but if your platform compiler happens to be using a character set that does not have the letters of the alphabet in sequence then this wont hold true.

        Comment

        • apsonline
          New Member
          • Aug 2006
          • 10

          #5
          i tried this :
          the problem is if the input is (say)aaass
          the output it shows is a3s4
          the if loop is working fine but m not able to make the value of ct to back to 1 if a match doesn occur

          #include<conio. h>
          #include<string .h>
          #include<stdio. h>
          #include<iostre am.h>
          void main()
          {
          clrscr();
          int i,j,ct=1;
          char c,a[100];
          cout<<"Enter"<< "\n";
          cin>>a;
          cout<<"\n"<<str len(a);
          for(i=0;i<strle n(a);i++)
          {
          if( a[i]==a[i+1])//compare the first letter with the second letter of the string
          ct=ct+1;//if there is a match,then increment ct to 2

          else

          cout<<a[i]<<ct;//print out the previous letter along with ct

          }

          getch();
          }

          Comment

          • MrJay
            New Member
            • Aug 2006
            • 6

            #6
            Code:
            char* str = new char[100];
            	char* temp = str;
            	char* read = new char[strlen(str)+1];
            
            	cout<<"Enter the string"<<endl;
            	cin>>temp;	
            	int count = 0, i=0;
            	char* p = &temp[0];
            	while(*temp && p++)
            	{
            		read[i++] = *temp;
            		if(*temp == *p)
            		{
            			count++;						
            			temp++;			
            			continue;
            		}	
            		read[i] = '\0';
            		cout<<read<<count+1;		
            		count = 0;
            		i = 0;
            		temp++;
            	}
            	delete []str;
            	delete []read;
            	cout<<endl;

            Comment

            • rgb
              New Member
              • Aug 2006
              • 37

              #7
              please refer to this link for some idea plus some tweaking to achieve your target:

              Comment

              • Rakesh Mutharaju
                New Member
                • Sep 2006
                • 14

                #8
                for the first question try doing this..
                1. check for the reappearance of each char in the given string.
                2. sort the list based on the char, check the value of the reappearance is well associated when you sort.

                for the sencond question, small correction in the solution you have tired.

                else
                {
                cout<<a[i]<<ct;//print out the previous letter along with ct
                ct =1; // reset the ct value after printing..
                }

                try this.. 'ill provide the solution if not..

                Comment

                • apsonline
                  New Member
                  • Aug 2006
                  • 10

                  #9
                  hey rakesh

                  the else part u suggested aint working:(

                  Comment

                  • MrJay
                    New Member
                    • Aug 2006
                    • 6

                    #10
                    Hi aspoline,
                    I have sent you the working code. Try this out.

                    It works fine as you want

                    Code:
                    char* str = new char[100];
                    	char* temp = str;
                    	char* read = new char[strlen(str)+1];
                    
                    	cout<<"Enter the string"<<endl;
                    	cin>>temp;	
                    	int count = 0, i=0;
                    	char* p = &temp[0];
                    	while(*temp && p++)
                    	{
                    		read[i++] = *temp;
                    		if(*temp == *p)
                    		{
                    			count++;						
                    			temp++;			
                    			continue;
                    		}	
                    		read[i] = '\0';
                    		cout<<read<<count+1;		
                    		count = 0;
                    		i = 0;
                    		temp++;
                    	}
                    	delete []str;
                    	delete []read;
                    	cout<<endl;
                    Thanks

                    Comment

                    • Rakesh Mutharaju
                      New Member
                      • Sep 2006
                      • 14

                      #11
                      #include<conio. h>
                      #include<string .h>
                      #include<stdio. h>

                      void main()
                      {
                      int i,j,ct=1;
                      char c,a[100];
                      clrscr();
                      printf(" Enter the string: ");
                      scanf("%s",a);
                      printf("\n string length %d \n",strlen(a) );
                      printf("Output :");
                      for(i=0;i<strle n(a);i++)
                      {
                      if( a[i]==a[i+1])
                      ct=ct+1;
                      else
                      {
                      printf("%c%d",a[i],ct);
                      ct = 1;
                      }

                      }

                      getch();
                      }


                      Hey this is working i tried and gave u that suggestion.. check out!! .. hey did u check that there is a flower brace for the else part..


                      i even have a simple C solution for the first question.. will post sometime later this week,. meanwhile u give a try :)
                      Last edited by Rakesh Mutharaju; Sep 4 '06, 03:01 AM. Reason: wanted to add more explanation..

                      Comment

                      Working...