-
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;
}
-
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++;
}
}
-
the task is to remove any spaces, and duplicate lattersm so something like
"this yellow bannana" will become "thisyelowbanana"
what is the "memset" for?
-
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++;
}
}
-
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?
-
is there a way to get around that?