Check if a letter exists in the c++ string

Asked

Viewed 1,682 times

0

I’m having problems in my function that checks the existence of a specific letter in a string (I don’t know much about pointers I only used because my other function wasn’t returning the string if it wasn’t by pointer.)

How I declared my string within the class:

char *nome[tamanho];

My job:

int Charclass::verificar_letra_existente(){
char *a;
int i;
int valorretornado;

printf("digite uma letra:\n");

scanf("%c",*a);

for(i=0;i<tamanho;i++)
{
  if(*nome[i]==*a){
    printf("ACHAMOS A LETRA NA STRING, ESTA NA POSICAO:");
    return i;
  }
}
printf("NAO ACHAMOS UMA LETRA NA STRING :\n");
}
  • What mistakes have appeared?

  • it asks the letter to check the string, when I type it closes the program, it doesn’t even check if it exists in the string. @v.Santos

  • tamanho is a global constant? What value are you starting it with? Edit your question to put also, at least, the class constructor charClass. It would also help if you put the snippet where you are instantiating the object of this class and the snippet that you are invoking the method verificar_letra_existente() . @Victor.

  • as I had said I don’t know how to work with pointers, my teacher told me to use pointers only in the function that returns string, so I won’t need the code anymore, thanks @v.Santos

  • format the code with indentation and spaces, otherwise it becomes more difficult to understand...

1 answer

1


This code has nothing of C++, taking the declaration of the function.
You have made several basic mistakes. I corrected below but have not tested the operation.

int Charclass::verificar_letra_existente() {
  // char *a; // <-----
  char a;
  int i;
  int valorretornado;

  printf("digite uma letra:\n");

  // scanf("%c", *a); // <---
  scanf(" %c", &a);

  for (i = 0; i < tamanho; i++)
  {
    // if (*nome[i] == *a) { // <--- onde e' que foi declarado "nome" ???
    if (nome[i] == a) { // <--- onde e' que foi declarado "nome" ???
      printf("ACHAMOS A LETRA NA STRING, ESTA NA POSICAO:");
      return i;
    }
  }

  printf("NAO ACHAMOS UMA LETRA NA STRING :\n");
}
  • If you say the code is not in c++ because you have scanf and printf? you are mistaken, I am in a class so yes I am in c++. (I just did not leave explained in the question to not get enormously =p)

  • Thanks for the reply, so it’s working properly =)

  • I don’t know how to use pointers so I made it clear soon, I’ll wait a little to work with pointers.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.