First off, there's a mistake in this line:
Code:
scanf("%s %s",&str1,&str2);
Should be:
Code:
scanf("%s %s",str1,str2);
No need for the & specifier when dealing with arrays - if you were reading in characters or numbers into single variables it would be different, e.g.
Code:
int number = 0;
scanf ("%d", &number);
Although you really shouldn't be using scanf to read in strings - fgets is much safer - see my sig for an example of how to use fgets.
Secondly, you really need to look at your loop more closely:
Code:
for(i=0;*str[i]!='\0';i++,j++){
if(*str[i]!=str1[j]){
continue;
}
If you enter the word "We" to be stored in str1, take a look at where the individual characters of that word go:
Code:
str1[0] = 'W'
str1[1] = 'e'
str1[2] = '\0'
In your loop code you use the variable j to access the individual elements of str1. However, you should not access any elements after str1[2]. You should insert an extra check so that you do not go past the termination character '\0' in str1. Something involving comparing j against the value n should be useful.
Also, you might want to look at comparisons involving *str[i]. When you declared str you actually created an array of strings, such that str[i] will access the i-th string. Try out something like the following to see my point:
Code:
printf ("%s\n", str[3]); /*Should print 'Erase the past'*/
However, when you add the * symbol in front of it, you only get the first character of string number i. Again for demonstration, try this:
Code:
printf ("%c\n", *str[3]); /*Should print 'E' only*/
Personally I like to think of arrays of strings as 2d arrays of characters, so I might do something like this to go through each element of the array:
Code:
for (i = 0; i < 6; i++) /*The number 6 is here because we happen to have declared an array of 6 strings*/
{
for (j = 0; str[i][j] != '\0'; j++)
{
/*Insert code to compare current character with characters being sought*/
}
}
That should point you in the right direction at least.
Lastly, you probably want make your code more general, so that you can replace words other than We. At the moment your attempt appears to try to find the first character in str that matches up with the first character of str1, and if it does, it checks the next 2 characters. It would probably be nicer to use a loop for this bit, if you can figure out a way to do that.