Write a nonrecursive function which will eliminate the duplicate characters in a
ID: 3650603 • Letter: W
Question
Write a nonrecursive function which will eliminate the duplicatecharacters in a null terminated string. Assume that the string consists of only
lower case letters. The solution should be implemented with pointers.
Examples : Given string : arsefert, Modified String : arseft
Given string : bb Modified String : b
Note: In this problem you are not allowed to use any other character array
than the given one. Further, the use of library string functions are not
allowed in this problem
this is what i have so far but doesnt seem to work!!
#include <iostream.h>
void duplicate ( char * );
int main( )
{
char stringarray[ ] = "arsefert";
cout << stringarray << endl;
duplicate( stringarray);
cout << stringarray << endl;
return 0;
}
void duplicate ( char *string );
{
char *temp;
int x;
// From the beginning of the string scan the string for duplicate characters.
while ( *string != '' ) {
temp = string;
temp++;
// Scan the remainder of the string to determine if there is a duplicate of the character pointed by the pointer string.
while ( *temp != '' ) {
if ( *temp == *string ) {
// A duplicate character is found, move each character one position to the left.
x = 1;
while ( *(temp+x) != '' ) {
*(temp+x-1) = *(temp+x);
x++;
}
// Move also the null character to the left.
*(temp+x-1) = *(temp+x);
}
temp++;
}
string++;
}
}
Explanation / Answer
please rate - thanks
would you believe, all I did was get rid of the extra ;
#include <iostream.h>
void duplicate ( char * );
int main( )
{
char stringarray[ ] = "arsefert";
cout << stringarray << endl;
duplicate( stringarray);
cout << stringarray << endl;
system("pause");
return 0;
}
void duplicate ( char *string ) //;
{
char *temp;
int x;
// From the beginning of the string scan the string for duplicate characters.
while ( *string != '' ) {
temp = string;
temp++;
// Scan the remainder of the string to determine if there is a duplicate of the character pointed by the pointer string.
while ( *temp != '' ) {
if ( *temp == *string ) {
// A duplicate character is found, move each character one position to the left.
x = 1;
while ( *(temp+x) != '' ) {
*(temp+x-1) = *(temp+x);
x++;
}
// Move also the null character to the left.
*(temp+x-1) = *(temp+x);
}
temp++;
}
string++;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.