473,395 Members | 1,474 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

can u guys help me out?
Aug 28 '06 #1
10 9731
Banfa
9,065 Expert Mod 8TB
Have an attempt yourself first, then we will help you debug it.
Aug 28 '06 #2
rgb
37
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
9,065 Expert Mod 8TB
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
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
MrJay
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
rgb
37
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
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
hey rakesh

the else part u suggested aint working:(
Sep 1 '06 #9
MrJay
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
#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

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

Similar topics

4
by: Dim | last post by:
I found that C# has some buggy ways to process string across methods. I have a class with on global string var and a method where i add / remove from this string Consider it a buffer... with some...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
29
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return...
4
by: WaterWalk | last post by:
Hello, I'm currently learning string manipulation. I'm curious about what is the favored way for string manipulation in C, expecially when strings contain non-ASCII characters. For example, if...
10
by: micklee74 | last post by:
hi if i have a some lines like this a ) "here is first string" b ) "here is string2" c ) "here is string3" When i specify i only want to print the lines that contains "string" ie the first...
5
by: Niyazi | last post by:
Hi, Does anyone knows any good code for string manipulation similar to RegularExpresion? I might get a value as string in a different format. Example: 20/02/2006 or 20,02,2006 or ...
3
by: crprajan | last post by:
String Manipulation: Given a string like “This is a string”, I want to remove all single characters( alphabets and numerals) like (a, b, 1, 2, .. ) . So the output of the string will be “This is...
7
Frinavale
by: Frinavale | last post by:
I currently have a .NET application that has an object which passes a string (a connection string) as a parameter to another object that does database manipulation. This string isn't stored...
3
by: frankeljw | last post by:
I have 2 Java strings 1st String is a series of names, colons, and numbers ie) Name1:13:Name2:4526:Name3:789:Name4:3729:Name5:6:Name6:44 2nd String is a name ie) Name2 I need to get the...
22
by: mann_mathann | last post by:
can anyone tell me a solution: i cannot use the features in standard c++ string classgh i included the string.h file but still its not working.
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.