The function should calculate the size of a string, passing a string as a parameter

Asked

Viewed 193 times

-1

#include<stdio.h>
#include<string.h>

/*Função que verifica tamanho da string*/
int conta_str(char x){

    int i, tamanho = 0;
    char str[40];
        do{

            gets(str);

                for(i = 0, tamanho = 0 ; i < 30 ; i++){
                    if(str[i] == '\0'){
                        break;
                    }
                    tamanho++;
                }

            if(tamanho < 8){
                printf("Tamanho inferior a 8 caracteres...");
            }
        }while(tamanho < 8);



return tamanho;
}


int main(){
    char str[40];
    gets(str);

    conta_str(str);

return 0;
}
  • 4

    What exactly do you want to do? I also suggest you read the community guidelines on how to make a good question.

  • Opa Paulo. So it is difficult to help you, edit your question and explain what you are trying to do and what mistake is happening.

  • You set your function with the parameter being a single character (char x) but call the function by passing an array of conta_str(str) characters, and your function does absolutely nothing with the parameter.

  • Excuse the ignorance gnt. I’m crawling in this world of programming.

  • @Paulomaciel read the link I sent you, come back here and correct!

1 answer

-1


A possible alternative:

#include <stdio.h>
#include <string.h>

/*Função que verifica tamanho da string*/
int conta_str(char x[]){
    int i=0;
    while (x[i] != '\0')
        i++;
    if (i < 8) {
        printf("Tamanho inferior a 8 caracteres...");
        return -1;
    }
   else
        return i;
}


int main(){
    char str[40];
    int tamanho;
    gets(str);
    tamanho = conta_str(str);
    if (tamanho == -1)
        printf("Tamanho inferior a 8 caracteres...");
    else
        printf("Tamanho de %s: %d\n", str, tamanho);
    return 0;
}
  • Thank you very much. it seems that in this case it was only pass the vector as parameter and not a single character. Valew by help

Browser other questions tagged

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