|
-
Apr 20th, 2002, 05:33 PM
#1
Thread Starter
Fanatic Member
whats wrong with this?
We were given sample problems for a programming competition, all of them seem pretty easy. But I cant get this to work, and can't figure out why..
Code:
int main(int argc, char* argv[]) {
char *strDef = new char[50]; //first string
char *tmp; //string to hold temporary
register int i; //varable to hold loop
printf("Please enter a sentence and press <ENTER>: ");
gets(strDef);
char *strNew = new char[strlen(strDef)];
if(!strNew) //make sure its valid
return 0;
for(i=0;i<=strlen(strDef);i++){
//begin checking
if(strDef[i]==' ')
i++;
else if(strDef[i] == strDef[i+1]){ //check if there are two letters in a row
strcat(strNew,(char*)strDef[i]);
i+=2;
} else{
strcat(strNew,(char*)strDef[i]);
//strNew+=strDef[i];
}
}
printf("Here is the formatted string: %s", strNew);
return 1;
}
Visit www.fragblast.com
Gaming, forums, and a online RPG/Battle system
(__Flagg) DOT NET? is this a Hindi Dating service?
-
Apr 20th, 2002, 07:09 PM
#2
It looks like you are trying to remove duplicates
Code:
void dedupe(char *src, char *dest){ // dest will be the finished product
char *buf;
char *s
s=dest;
buf = src;
memset(s,0x00,strlen(src)+1);
if(!strlen(buf) return;
*s=*buf++;
while (*buf!=0x00){
if(*buf != *s) {
s++;
*s=*buf;
}
buf++;
}
}
-
Apr 20th, 2002, 07:22 PM
#3
Thread Starter
Fanatic Member
the task is to remove any spaces, and duplicate lattersm so something like
"this yellow bannana" will become "thisyelowbanana"
what is the "memset" for?
Visit www.fragblast.com
Gaming, forums, and a online RPG/Battle system
(__Flagg) DOT NET? is this a Hindi Dating service?
-
Apr 20th, 2002, 07:27 PM
#4
memset makes the string zero-character filled.
You want:
Code:
void dedupe(char *src, char *dest){ // dest will be the finished product
char *buf;
char *s
s=dest;
buf = src;
memset(s,0x00,strlen(src)+1);
if(!strlen(buf) return;
*s=*buf++;
while (*buf!=0x00){
if(*buf== 0x20) {
buf++;
continue;
}
if(*buf != *s) {
s++;
*s=*buf;
}
buf++;
}
}
-
Apr 20th, 2002, 07:40 PM
#5
Thread Starter
Fanatic Member
i guess 0x20 is a space?
so its make copies of the original string, then itll loop through and check whether is a space...or is 0x20 the thing to loop at the next character?
just wondering though, why wouldnt my method work?
Visit www.fragblast.com
Gaming, forums, and a online RPG/Battle system
(__Flagg) DOT NET? is this a Hindi Dating service?
-
Apr 23rd, 2002, 01:07 PM
#6
Thread Starter
Fanatic Member
is there a way to get around that?
Visit www.fragblast.com
Gaming, forums, and a online RPG/Battle system
(__Flagg) DOT NET? is this a Hindi Dating service?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|