Advertisement

Scanning strings

Started by October 19, 2000 02:38 PM
4 comments, last by SKSlayer 24 years, 3 months ago
How do I scan a string to eliminate a character or remplace it (And NO, I can''t acces MSDN cuz my CD drive is broken )
(you can find me on IRC : #opengl on undernet)
Wow, sucks to be you with a broken cd drive. j/k...

Should be something like... (if you''re only scanning for single characters and not a sequence of them...)

    char MyString[] = "Hello, I am a happy string";//cout << MyString; // outputs Hello, I am a happy string//for (int i = 0; i < strlen(MyString); i++){  if (MyString<i> == ''h'')  {    MyString[i] = ''s'';  }  else if (MyString[i] == ''p'')  {    for (int j = i; j < strlen(MyString); j++)    {       if (j == strlen(MyString) - 1)         MyString[j] = ''\0'';       else         MyString[j] = MyString[j + 1];    }  }  else if (MyString[i] == ''y'')  {    MyString[i] = ''d'';  }}//cout << MyString; // technically this should output Hello, I am a sad string    


And, no, the Hello should not be Sello, because it''s scanning for lowercase ''h''... I think that should do it... Try it out.

S.
Advertisement
Just to make a point that everything can be done diferent ways (or just to get a reason to post a reply you could with the same effect use a switch/case statement (I prefer doing so in cases like this because the code is easier to read)
int len = strlen(Mystring); //faster to precalculate for(int i=0; i < len; i++){  switch(Mystring) {    case ''h'' :      Mystring = ''s'';<br>      break;<br>    case ''y'' :<br>      Mystring = ''d'';<br>      break;<br>    case ''p'' :<br>      for(j = i; j < len - 1; j++)<br>          Mystring[j] = Mystring[j + 1];<br>      Mystring[len - 1] = ''\0'';<br>      break;<br>   } // switch<br>} // for i<br></pre><br><br>So there… :-)  </i>   
Sorry Strylinys, but your code out puts "Hello, I am a sapd string" And as far as I know I haven''t ever seen a sapd string walking around here--how about up in Canada
the problem with replacing words in a string is that if the length of the string grows you have to make sure you allocate more memory for it, or you'll run into an overflow error or lose some of your string.

you might try something like this

    #include <stdio.h>#include <string.h>#include <stdlib.h>char* replace (char* source, char* out, char* in) {  int ls = strlen(source);  int li = strlen(in);  int lo = strlen(out);  int o = 0, d = 0; // counters for source and out  int found = -1;  int i, j;  char *dst = (char*)malloc(ls * sizeof(char));  for (i=0;i<=ls;i++) {    if (source<i> == out[o]) {	  if (found == -1) found = i;      o++;      if (o == lo) {        i = found;		if (lo < li) {			char *temp = (char*)malloc((ls + li - lo) * sizeof(char));			strcpy(temp, dst);			free (dst);			dst = temp;		}		for (j=0;j<li;j++) {			dst[i+j] = in[j];		}		i = found + lo - 1;		d += li;		found = -1;		o = 0;	  }	} else if (found >=0) {		i = found;		found = -1;	} else {		dst[d] = source[i];		d++;	}  }  return dst;}int main(void) {	char before[] = "Hello, i am a happy string.";	char *after		printf("%s\n", before);	after = replace (before, "happy", "sad");	printf("%s\n", after);	free(after);	return 0;}    


Edited by - ogapo on October 21, 2000 4:48:01 PM
Brought to you by: [email=ogapo@ithink.net]O.G.A.P.O.[/email] +----------------------------------------+| Surgeon General's Warning - || OGAPO can cause serious mental damage || if taken in large doses. |+----------------------------------------+/* Never underestimate the power of stupid people in large groups */
What?! You''ve never seen a sapd string??!??!!? Wow, you''re missing out...

No, seriously.. That was just off of the top of my head (not too good these days. )

It should have this in there (I think you can figure out where...)

                else if (MyString<i> == ''p''){  for (int j = i; j < strlen(MyString); j++)  {    if (j == strlen(MyString) - 1)      MyString[j] = ''\0'';    else      MyString[j] = MyString[j + 1];  }  // the next line is the one that should be added...  i--; // this one...  // the one above this comment...}                


There, NOW it''s a sad string... But it''s much more interesting when we are able to examine the sapd string in its natural environment.

S.

This topic is closed to new replies.

Advertisement