Function to print the number of characters in a two-dimensional string

Asked

Viewed 189 times

0

By the enunciation of the exercise, I must use as the second argument char **strings, I believe that there is my confusion, in manipulating her to count the size of string. (I did other tests with the second argument as char strings[][str_size] and everything went well, but as I said, I need to use the argument as being char **strings.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define str_size 127

void imprimeTamString(int numStrings, char **strings)
{
    int i, j;
    int tamanho;

    printf("\n---Tamanhos---\n\n");
    for(i=0; i<numStrings; i++)
    {
        tamanho=0;
        for(j=0; j<str_size; j++)
        {
            if(strings[i][j]!='\0')
                tamanho=tamanho+1;
            else
                break;
        }
        printf("String %d - Tamanho = %d\n", i+1, tamanho);
    }
}

int main()
{
    int n;//numero de strings//

    printf("Deseja entrar com quantas strings? ");
    scanf("%d", &n);

    char *str1, **str2;
    char string[n][str_size];
    int i;

    for(i=0; i<n; i++)
    {
        printf("String %d = ", i+1);
        fflush(stdin);
        gets(string[i]);
    }

    str1=string[0];
    str2=&str1;

    imprimeTamString(n, str2);

    return 0;
}
  • And is there some restriction not to use the strlen()?

  • No, but I also could not do using strlen()

  • Call with: printTamString(n, string); you will be calling the function with a pointer to a char pointer.

  • @Marcoantonioschneider and has to be char**? Some reason to be yes: You’re not interpreting it this way but it might be another?

3 answers

2

You can simplify the code well and avoid some errors. In addition to not using a function that is inappropriate for reading data. But what’s complicating it is that you’re creating a array in the stack and then reading as pointer pointers. If you can change to read as array of strings will work:

#include <stdio.h>
#include <string.h>
#define STR_SIZE 127

void imprimeTamString(int numStrings, char (*strings)[STR_SIZE + 1]) {
    printf("\n---Tamanhos---\n");
    for (int i = 0; i < numStrings; i++) printf("String %d - Tamanho = %d\n", i + 1, (int)strlen(strings[i]));
}

int main() {
    int numStrings;
    printf("Deseja entrar com quantas strings? ");
    scanf("%d", &numStrings);
    scanf("%*c");
    char strings[numStrings][STR_SIZE + 1];
    for (int i = 0; i < numStrings; i++) {
        printf("String %d = ", i + 1);
        fgets(strings[i], STR_SIZE, stdin);
        strings[i][strcspn(strings[i], "\n")] = 0;
    }
    imprimeTamString(numStrings, strings);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

If you have to be a pointer you can do so, but then you have to allocate the strings in the heap:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STR_SIZE 127

void imprimeTamString(int numStrings, char **strings) {
    printf("\n---Tamanhos---\n");
    for (int i = 0; i < numStrings; i++) printf("String %d - Tamanho = %d\n", i + 1, (int)strlen(strings[i]));
}

int main() {
    int numStrings;
    printf("Deseja entrar com quantas strings? ");
    scanf("%d", &numStrings);
    scanf("%*c");
    char *strings[numStrings];
    for (int i = 0; i < numStrings; i++) {
        printf("String %d = ", i + 1);
        strings[i] = malloc(STR_SIZE + 1);
        fgets(strings[i], STR_SIZE, stdin);
        strings[i][strcspn(strings[i], "\n")] = 0;
    }
    imprimeTamString(numStrings, strings);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

1


Greetings

Your character count function is working correctly. What is occurring there is that you are not allocating properly the matrix that will store your String.

This is a very common mistake made by people who usually work with languages that the mere fact of declaring a variable already implies in their memory allocation, in C is not how it works.

What you will have to do BEFORE you start using your matrix called string is INITIALIZE each element with the appropriate number of bytes to store the strings that will be typed.

What you do is something like this:

char* string[n];
for(int i=0; i<n; i++) {
  string[i] = (char*) malloc(sizeof(char)*str_size);
}

Passing the parameter to the function is correct. You do not need to use & when it is string since the variable name already points to the pointer where it has the first character of the string!

Good luck with your exercise!

  • 1

    Thank you very much, the problem was even the lack of allocation

1

You need to understand some basic concepts before, which are:

What is a matrix?

Matrix is a vector-type data structure with two or more dimensions. The items of a matrix must all be of the same data type. In practice, the matrices form tables in memory.

How to make the Matrix Declaration?

You have some ways to declare the matrix, which are:

  • 1) char matriz[LINHA][COLUNA]
  • 2) int **matriz

Where the (1) would be the same thing as the (2), however the (1) you are obliged to initialize them with their sizes(LINHA & COLUNA). Soon the (2) no need and you can use the same to do dynamic allocation.

Other Forms of Matrix Declaration:

If you do not need an input (input) of data for insertion in the matrix, that is, you have the fixed data and that will always be constant, you can do so:

const char matriz[5][5] = { {'A', 'B', 'C', 'D', 'E'}, { 'F', 'G', 'H', 'I', 'J' } }

How to access Matrix elements?

You will need an algorithm O(n²), that is, two are nested, one for the LINHA and another to the COLUNA, as an example:

const char matriz[5][5] = { {'A', 'B', 'C', 'D', 'E'}, { 'F', 'G', 'H', 'I', 'J' } }

for(int linha = 0; linha < 5; linha++) {
    for(int coluna = 0; coluna < 5; coluna++) {
        //CÓDIGO
    }
}

Use the library <string> to check its size, for example:

string str ("Testando String");
cout << "TAMANHO DA STRING: " << str.size() << " bytes.\n";

Browser other questions tagged

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