Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   C Function To Replace A String In A String. (http://www.programmingforums.org/showthread.php?t=1529)

Scrag Dec 13th, 2004 9:27 AM

I've been trying to figure this one out for a while and my brain is fried. Basically, Im trying to write a function that will remove the "%20" thats represents a space in html and relace it with "\ ". Im trying this using the strstr() function, but I really dont know how to start this, or what I need for a basic algorithm. Any thoughts or ideas would be greatly appreciated, or even just a point in the starting direction.

THANKS!
Scrag

plox Dec 13th, 2004 3:49 PM

Sounds like a fun function. One question and then I would LOVE to help you. Will you be reading an external file or is the user going to be inputing the text into the command line? I might still be able to help if it is with the external file, that just makes things a tad harder. Only because I have not done much with external files, and so I don't have the most experience.

Benoit Dec 13th, 2004 4:05 PM

It would be easy with an external file...Just use fgets and read one line at a time :)

Scrag Dec 13th, 2004 4:32 PM

I am writing a web server, I got the web server part pretty much done, but Im adding the ability for the web server to list a directory for downloading files if needed. This will be a function that will be passed a string variable. For example, it gets passed the http string "Cool%20File.mpg", then I need to replace the http space "%20" with "\ " so the unix file system can read the file/cd to the dir. So after the function gets passed the string "Cool%20File.mpg", it should return the string "Cool\ File.mpg"

Thanks for the help!! :)

tempest Dec 13th, 2004 4:41 PM

If you're writing a webserver than you need to do more than convert just %20. You have to convert every number that appears after a % to its hexadecimal -> character value. Not just 20 for space..

amado Dec 13th, 2004 7:59 PM

Don't know what platform u're writing the program in but there is a function in msdn that does this for you ::: InternetCanonicalizeUrl

Scrag Dec 13th, 2004 10:05 PM

Yeah, i knoiw I need to convert all number values, but I figured this would be a good start :) I am using C and compiling with gcc, under linux. Im think in gonna write the function to read each char of the string to a new buffer string, then when strstr(httpGET_PATH, "%20), wite out a `\` and a ' ' char, skip 3 chars, continue etc. Any suggestions on a better way would be cool.

Thanks again,
Scrag

tempest Dec 13th, 2004 10:33 PM

There may or may not be a better way to do the if evals... but heres code for %20 that could be easily adapted....

:

void convertURL(char *blah) {
  char url[1024]; // If you have URLs bigger than 1024, you have problems...
  strcpy(url, blah);

  for(int len=0;len<1024;len++) if(url[len] == NULL) break;

  char newStr[1024];
  int pos = 0;
  for(int i=0;i<len - 3;i++) {
    if(url[i] == '%' &&
      url[i+1] == '2' &&
      url[i+2] == '0') {
      newStr[pos++] = ' ';
      i += 2;
    } else
      newStr[pos++] = url[i];
  }

  strcpy(blah, newStr);
}


Scrag Dec 13th, 2004 11:50 PM

Thanks a lot tempest!!! Definetly gets me going in the right direction :)
I've been struggling with this for a while now, and other forums didnt seem to help.

Thanks again!

:rock:

Scrag Dec 14th, 2004 12:27 AM

On second thought/compile, it works flawlessly,

Thanks again!

:mrgreen:


All times are GMT -5. The time now is 4:04 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC