By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,189 Members | 904 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,189 IT Pros & Developers. It's quick & easy.

string manipulation

P: 10
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:aaaabbbbcccdddd
output:a4b4c3d4

can u guys help me out?
Aug 28 '06 #1
Share this Question
Share on Google+
10 Replies


Banfa
Expert Mod 5K+
P: 8,916
Have an attempt yourself first, then we will help you debug it.
Aug 28 '06 #2

P: 37
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.
Aug 29 '06 #3

Banfa
Expert Mod 5K+
P: 8,916
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.
Aug 29 '06 #4

P: 10
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<iostream.h>
void main()
{
clrscr();
int i,j,ct=1;
char c,a[100];
cout<<"Enter"<<"\n";
cin>>a;
cout<<"\n"<<strlen(a);
for(i=0;i<strlen(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();
}
Aug 30 '06 #5

P: 6
Expand|Select|Wrap|Line Numbers
  1. char* str = new char[100];
  2.     char* temp = str;
  3.     char* read = new char[strlen(str)+1];
  4.  
  5.     cout<<"Enter the string"<<endl;
  6.     cin>>temp;    
  7.     int count = 0, i=0;
  8.     char* p = &temp[0];
  9.     while(*temp && p++)
  10.     {
  11.         read[i++] = *temp;
  12.         if(*temp == *p)
  13.         {
  14.             count++;                        
  15.             temp++;            
  16.             continue;
  17.         }    
  18.         read[i] = '\0';
  19.         cout<<read<<count+1;        
  20.         count = 0;
  21.         i = 0;
  22.         temp++;
  23.     }
  24.     delete []str;
  25.     delete []read;
  26.     cout<<endl;
Aug 30 '06 #6

P: 37
rgb
please refer to this link for some idea plus some tweaking to achieve your target:
http://www.cplusplus.com/ref/cstring/strchr.html
Aug 30 '06 #7

P: 14
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..
Sep 1 '06 #8

P: 10
hey rakesh

the else part u suggested aint working:(
Sep 1 '06 #9

P: 6
Hi aspoline,
I have sent you the working code. Try this out.

It works fine as you want

Expand|Select|Wrap|Line Numbers
  1. char* str = new char[100];
  2.     char* temp = str;
  3.     char* read = new char[strlen(str)+1];
  4.  
  5.     cout<<"Enter the string"<<endl;
  6.     cin>>temp;    
  7.     int count = 0, i=0;
  8.     char* p = &temp[0];
  9.     while(*temp && p++)
  10.     {
  11.         read[i++] = *temp;
  12.         if(*temp == *p)
  13.         {
  14.             count++;                        
  15.             temp++;            
  16.             continue;
  17.         }    
  18.         read[i] = '\0';
  19.         cout<<read<<count+1;        
  20.         count = 0;
  21.         i = 0;
  22.         temp++;
  23.     }
  24.     delete []str;
  25.     delete []read;
  26.     cout<<endl;
Thanks
Sep 1 '06 #10

P: 14
#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<strlen(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 :)
Sep 4 '06 #11

Post your reply

Sign in to post your reply or Sign up for a free account.