8 Replies - 379 Views - Last Post: 08 January 2012 - 11:15 AM Rate Topic: -----

#1 amit2011  Icon User is offline

  • D.I.C Head

Reputation: -13
  • View blog
  • Posts: 64
  • Joined: 17-March 11

error in string replace(replacing word from a string)

Posted 08 January 2012 - 09:20 AM

input= this is a example, I like studying example
I want to replace each occurence of word example with word program

OUTPUT should be= this is a program, I like studying program

I have coded it, but some error is coming and i think there is come problem in my code.
Cn anybody suggest me smthng??
thank you


#include <stdio.h>
#include <string.h>
char replace(char[],char[],char[]);

void main()
{
 char mystr[50] = "this is a example, I like studying example";
 char old[]="example";
 char neww[]="language";
 char c[40]="\0";
 clrscr();
 puts(mystr);

 //puts(replace(mystr[], old[], neww[]));

 c=replace(mystr[],old[],neww[]);
 //puts(c);

 getch();
}

char replace(char mystrr[], char oldd[], char newww[])
{

int a,i,j;
a=strlen(mystrr);

 for(i=0;i<a;i++);
 {
int a,i,j;
a=strlen(mystrr);

 for(i=0;i<a;i++);
 {
  for(j=0;j<(mystrr!=0);j++)
   {
     if(mystrr[i]==oldd[i])
      {
	mystrr[i]=newww[i];
      }

   }
 }

 //puts(mystrr);
return mystrr;




Is This A Good Question/Topic? 0
  • +

Replies To: error in string replace(replacing word from a string)

#2 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 215
  • View blog
  • Posts: 586
  • Joined: 04-August 09

Re: error in string replace(replacing word from a string)

Posted 08 January 2012 - 09:31 AM

On line 16 the [] shouldn't be there.
c=replace(mystr,old,neww);


The return type of replace is char but you're returning an array.

Your missing some closing braces '}' at the end.

This post has been edited by simeesta: 08 January 2012 - 09:34 AM

Was This Post Helpful? 0
  • +
  • -

#3 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: error in string replace(replacing word from a string)

Posted 08 January 2012 - 09:36 AM

Well, to begin with, you need to copy & paste more carefully. Lines 30-34 are an exact duplication of lines 25-29. That should make you wonder ...

Also, if you're writing a C program, you have to use C functions. You can't compare strings in C using ==, and you can't assign strings using =. Study the C string functions strcmp and strcpy.

That should get you started, I hope...
Was This Post Helpful? 0
  • +
  • -

#4 amit2011  Icon User is offline

  • D.I.C Head

Reputation: -13
  • View blog
  • Posts: 64
  • Joined: 17-March 11

Re: error in string replace(replacing word from a string)

Posted 08 January 2012 - 09:38 AM

View Postsimeesta, on 08 January 2012 - 09:31 AM, said:

On line 16 the [] shouldn't be there.
c=replace(mystr,old,neww);


The return type of replace is char but your returning an array.

Your missing some closing braces '}' at the end.


thnks for rplyng.. :)

I did wht u said.
Now it is showing an error at line 16 that L VALUE REQUIRED....
and at 2nd last line i.e at return mystr; the error is "non portable conversion"

wht shld i do now??
Was This Post Helpful? 0
  • +
  • -

#5 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: error in string replace(replacing word from a string)

Posted 08 January 2012 - 09:40 AM

Oh, and what is the string char c[40]="\0"; for? You haven't used it. I hope you weren't planning to use it to contain the entire revised string -- that will get you in trouble since it's smaller than the original mystr[50].
Was This Post Helpful? 0
  • +
  • -

#6 amit2011  Icon User is offline

  • D.I.C Head

Reputation: -13
  • View blog
  • Posts: 64
  • Joined: 17-March 11

Re: error in string replace(replacing word from a string)

Posted 08 January 2012 - 09:43 AM

View Postr.stiltskin, on 08 January 2012 - 09:36 AM, said:

Well, to begin with, you need to copy & paste more carefully. Lines 30-34 are an exact duplication of lines 25-29. That should make you wonder ...

Also, if you're writing a C program, you have to use C functions. You can't compare strings in C using ==, and you can't assign strings using =. Study the C string functions strcmp and strcpy.

That should get you started, I hope...



thanks for telling me that in my code some lines are repettive!! :rolleyes2:

I very well knw abt strcmp, strcat, strlen, strstr etc...
but if i dn't wnt to use any string function
thn how should i continue now??? :look:
Was This Post Helpful? 0
  • +
  • -

#7 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 215
  • View blog
  • Posts: 586
  • Joined: 04-August 09

Re: error in string replace(replacing word from a string)

Posted 08 January 2012 - 09:43 AM

Try posting your new code, with the error messages you receive.
Was This Post Helpful? 0
  • +
  • -

#8 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: error in string replace(replacing word from a string)

Posted 08 January 2012 - 10:19 AM

View Postamit2011, on 08 January 2012 - 11:43 AM, said:

I very well knw abt strcmp, strcat, strlen, strstr etc...
but if i dn't wnt to use any string function
thn how should i continue now??? :look:

If you don't want to use any string functions, then you can't compare strings. In that case you have to compare each individual character, 1 at a time. And you have to copy from the original string to the new string one character at a time.
Was This Post Helpful? 0
  • +
  • -

#9 amit2011  Icon User is offline

  • D.I.C Head

Reputation: -13
  • View blog
  • Posts: 64
  • Joined: 17-March 11

Re: error in string replace(replacing word from a string)

Posted 08 January 2012 - 11:15 AM

ok, plcng my code again!!
Now wht should i do, to solve the error



#include <stdio.h>
#include <string.h>
char replace(char[],char[],char[]);

void main()
{
 char mystr[50] = "this is a example, I like studying example";
 char old[]="example";
 char neww[]="language";
 char c[60]="\0";
 //char ch[]="\0";
 clrscr();
 puts(mystr);

 //puts(replace(mystr[], old[], neww[]));

 c=replace(mystr,old,neww); // [b] Error: L value required [/b]
 puts(c);

 getch();
}

char replace(char mystrr[], char oldd[], char newww[])
{

int a,i,j;
a=strlen(mystrr);

 for(i=0;i<a;i++);
 {
  for(j=0;j<(mystrr!=0);j++)
   {
     if(mystrr[i]==oldd[i])
      {
	mystrr[i]=newww[i];
      }

   }
 }

 //puts(mystrr);
return mystrr;  //[b] error:nonportable converion [/b]

}





Was This Post Helpful? 0
  • +
  • -

Page 1 of 1