i need help about the String codes....

its about how to delete a string..
like for example
it displays like
"The Dogs and Cats"
the program wants the user to choose what to delete
like i want to delete the Dogs.... and the result is now
"The and Cats"
cause the Dogs is delete....

I can think of two super easy ways to do it:

  1. Use a second array and copy the parts that aren't deleted. All you need to do is find the start of the substring you want to delete and it's a matter of calling strcpy.
  2. Shift all contents of the array over the substring you want to delete. The shifting logic can be tricky for a beginner, but it's not especially difficult.

What have you tried so far?

there is a function "strstr" which returns a pointer to the first occurence of that string in another string. first find string using strstr then use strncpy to copy the the string before the pointer returned by strstr and after ptr + strlen (delete_word).
ptr is the pointer returned by strstr.

repeat these steps untill ststr return you null ensuring that there is no more occurrence of delete_word in this string.

I'm the subordinate of traced and i just lately create a code like i'm using the token but i dont really get about deleting the word coz its somewhat like delete the letters only..... but i need to delete words from the first array...... and the word to delete is written on the 2nd array.... further i dont understand what to do....

@NARUE
here's my code.....

#include<stdio.h>
#include<string.h>

int main(void)
{

char *p, *p1;

p=calloc(100, sizeof(char));
p1=calloc(100, sizeof(char));
printf("Enter string: ");
gets(p);
printf("Enter string to delete: ");
gets(p1);

strtok(p, p1);


getch();
return 0;
}

This is my code and after it i don't know further after strtok..... I use strtok because some says it is useful to it..... Please add how to delete word string.....

Edited 6 Years Ago by WaltP: Added CODE tags -- with all the help about them, how could you miss using them????

here is a code for deleting one occurence of that string.

#include <stdio.h>
#include <string.h>
#include <malloc.h>

int main ()
{
  char str[] = "This is a sample string";
  char del_word[] = "string";
  char * new_str =(char*)malloc(sizeof(str));
  char * pch=strstr(str,del_word);
  printf(" %s\n\n\n",pch);
  /*while (pch)
  {*/
	  str[pch-str] = '\0';
	  strncpy(new_str,str,pch-str+1);
	  pch = pch + strlen(pch)-1;
	  strcat(new_str,pch);
	  printf("%s",new_str);
	  /*pch=strrchr(str,' ');
	  if(!strrchr(str,' '))
	  {
		  printf(" %s",str);
		  break;
	  }
  }*/
  return 0;
}

>I'm the subordinate of traced
God help your company. :icon_rolleyes:

Comments
never try to discourage some one. I know that you are perfect in coding. But dont uneder estimate some one. its my request to you.

>I'm the subordinate of traced
God help your company. :icon_rolleyes:

man!! How can help if me too don't understand:((

Read Narue's first post (her second was for us, not you) and attempt to understand what she said.

This is my code and after it i don't know further after strtok..... I use strtok because some says it is useful to it.....

If someone said you should use strtok() and you have no idea how to use strtok() it is far from useful. And in fact that someone IMO was wrong anyway.

@adnan
hey ive tried your code and it works...
but it is a different code...
this code is somewhat like
it is asking the user to type a string like you type for example "The Dogs and Cats...
then after that in the second string it will ask you to what will you choose to delete like
i want to delete the word Dogs... then the result is
The and Cats.. like that....

@narue
yeah i used a second array.. but dont know anymore further....

Edited 6 Years Ago by traced: n/a

WaltP?

I understand about strtok coz it is use to search char within the string and make a token from what and where the delimiters place..... If you some idea about this show us but please don't blame me of low understanding because im a student and actually a newbie about string. Hope u understand, i'm not asking help to do my assignments i just want to understand about it...

@adnan
hey ive tried your code and it works...
but it is a different code...
this code is somewhat like

The idea is to use his working code to understand what he did. Then use that understanding to write your code. All you seem to be saying is "that code is not exactly what I'm trying to do, so write what I need for me."

WaltP?

I understand about strtok coz it is use to search char within the string and make a token from what and where the delimiters place..... If you some idea about this show us but please don't blame me of low understanding because im a student and actually a newbie about string. Hope u understand, i'm not asking help to do my assignments i just want to understand about it...

Of course I understand. I'm not blaming you for not knowing. I'm blaming whoever told you about strtok() and didn't explain it to you. What I am saying is you don't need strtok() at all.

I really can't help you because when I tried you ignored what I said completely.

This article has been dead for over six months. Start a new discussion instead.