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?
10 9731 Banfa 9,065
Expert Mod 8TB
Have an attempt yourself first, then we will help you debug it.
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.
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.
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();
}
- 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;
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..
hey rakesh
the else part u suggested aint working:(
Hi aspoline,
I have sent you the working code. Try this out.
It works fine as you want - 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
#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 :)
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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 ...
|
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...
|
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...
|
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...
|
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.
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
| |