-
what is wrong?
ok so it works fine when its one #
Code:
#include <iostream.h>
#include <string.h>
int main()
{
char story[100]="Bugsy Kludge is a # with our company.";
char findnouns[2]="#";
char nouns[10]="vegetable";
char copystory[100];
cout<<story<<endl;
int length=strlen(story), length2=strlen(nouns), count=0, count2=0, count3=0;
for(count=0;count<length;count++)
{
if(findnouns[0]==story[count])
{
strcpy(copystory,story);
for(count2=count;count2<(count+length2);count2++)
{
story[count2]=nouns[count3];
count3++;
}
length=length+length2;
story[count+length2]=' ';
for(count2=(count+length2+1);count2<length;count2++)
{
story[count2]=copystory[count2-length2+1];
}
}
}
cout<<story;
cout<<endl;
return (0);
}
so why wont it work when i have 2 #'s?
can someone help me this is really puzling
-
>> char findnouns[2]="#";
The size of the string is only 2. Hence it can only hold a single character and an end of string (/0 or NULL)
Every string must store a NULL character as it's last character...Hence when you want to store a word of 'n' characters (letters), you must initialize a string of 'n+1' characters.
-
nono thats not they way it works i search the array for the # then i replace the # with the word vegetable...
i do change the size of finnouns i only change the size of story
-
I looked over it and I don't really see anything wrong with it. Did it get a compile error? Or did it not run as expected? Did you add the \0 at the end of the array when you were done?
-
Try resetting count3 to 0 after it is used to copy nouns into story. If you don't do that, when you find a second # count3 will be of a value that is larger than the largest legal index for vegetable and you will be reading in garbage since you are "overreading" the array holding nouns.
-
nice! :P woot
Code:
#include <iostream.h>
#include <string.h>
int main()
{
char story[100]="Bugsy Kludge is a # with our company.";
char findnouns[2]="#";
char nouns[10]="vegetable";
char copystory[100];
cout<<story<<endl;
int length=strlen(story), length2=strlen(nouns), count=0, count2=0, count3=0;
for(count=0;count<length;count++)
{
if(findnouns[0]==story[count])
{
strcpy(copystory,story);
for(count2=count;count2<(count+length2);count2++)
{
story[count2]=nouns[count3];
count3++;
}
length=length+length2;
for(count2=(count+length2);count2<length;count2++)
{
story[count2]=copystory[count2-length2+1];
}
}
count3=0;
}
cout<<story;
cout<<endl;
return (0);
}