I'm new to this string area and I'm having a hard time. My algorithm must find and return the index of the first occurrence of the key (that the user types) in the string, otherwise it should return -1. What is wrong with my function?
#include <stdio.h>
#include <math.h>
#define MAX 100
int str_length(char str[])
{
int i =0;
while(str[i]!='#include <stdio.h>
#include <math.h>
#define MAX 100
int str_length(char str[])
{
int i =0;
while(str[i]!='%pre%')
{
i++;
}
return i;
}
int str_chave(char str[], int comp)
{
int i=0;
char chave;
printf("Informe um caractere: ");
scanf("%s",&chave);
for (comp; str[i] != chave; comp--)
{
if(str[i]==chave)
{
return 1;
}
str[i]--;
}
return 0;
}
int main()
{
char string[MAX];
int comp,res,resi,chave;
long long int n;
long long int number;
printf("Digite uma string: ");
scanf("%[^\n]s",string);
comp = str_length(string);
printf("Comprimento de '%s': %d\n",string,comp);
chave=str_chave(string,comp);
if(chave==0)
printf("O caractere informado existe na string.\n");
else
printf("O caractere informado nao existe na string.\n");
return 0;
}
')
{
i++;
}
return i;
}
int str_chave(char str[], int comp)
{
int i=0;
char chave;
printf("Informe um caractere: ");
scanf("%s",&chave);
for (comp; str[i] != chave; comp--)
{
if(str[i]==chave)
{
return 1;
}
str[i]--;
}
return 0;
}
int main()
{
char string[MAX];
int comp,res,resi,chave;
long long int n;
long long int number;
printf("Digite uma string: ");
scanf("%[^\n]s",string);
comp = str_length(string);
printf("Comprimento de '%s': %d\n",string,comp);
chave=str_chave(string,comp);
if(chave==0)
printf("O caractere informado existe na string.\n");
else
printf("O caractere informado nao existe na string.\n");
return 0;
}