-2

I need to calculate the size of a string in order to apply a function (the function applied is going to depend on the size of valor.)

However, as you can see in this example, I am having some trouble using strlen in the string (in the example you can see I inserted 2 'valor' and the given strlen was 6).

Here is the code, and next to it an image of the process returned.

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

int main() {
    char valor[5];
    char naipe[5];

    int c;

    int i = 0;
    do {
        c = getchar();
        if (((c > '0') && (c < '9')) || (c == 'K') || (c == 'Q') || (c == 'J') ||
            (c == 'A') || (c == 'T')) {
            valor[i] = c;
            continue;
        }
        if ((c > 'A') && (c < 'Z')) {
            naipe[i] = c;
            i++;
        }
    } while (c != '\n');

    printf("%ld", strlen(valor));

    return 0;
}

process box

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
user495758
  • 17
  • 3
  • Please apply some proper indendation on your code. Readability matters. – Gerhardh Apr 01 '20 at 09:59
  • 9
    You do not put a 0-terminator on your `valour` string. That causes undefined behaviour when calling string functions. – Gerhardh Apr 01 '20 at 10:00
  • 1
    `i` counts letters in `naipe` It does not match entries in `valour`. is that by intention? – Gerhardh Apr 01 '20 at 10:02
  • ok.. thanks for pointing me that! – user495758 Apr 01 '20 at 10:02
  • Gerhardh, I think it does because the valor of i is being incrementated repetadly, so when i ask to read the, for example, valor[3], its succesfully readen ( I tested it). – user495758 Apr 01 '20 at 10:07
  • 1
    You might have noticed that `valor` could contain unknown data. For example, if the string entered is "BBA" then `valor[2] == A` but `valor[0]` was never set, it's unknown junk (usually zero on a new stack / first run). – Myst Apr 01 '20 at 10:07
  • 1
    If you enter CKQ22 you will not have any content in `valor[0]`and `valor[1]`. – Gerhardh Apr 01 '20 at 10:08
  • BTW: `%ld` is used for `long int` but `strlen` returns a `size_t`. You should use `%zu` to print that. Mismatch od format specifier and parameter also causes undefined behaviour. – Gerhardh Apr 01 '20 at 10:16
  • 1
    Does this answer your question? [strlen: how does it work?](https://stackoverflow.com/questions/4132849/strlen-how-does-it-work) – vgru Apr 01 '20 at 10:20
  • @user495758 next time don't post pictures of text, but post text as text. – Jabberwocky Apr 01 '20 at 10:35

1 Answers1

1

you have two arrays and you should use two counters for them ,otherwise you would probably skip some elements of each arrays.

also you should terminate char valor[5] and char naipe[5] with '\0'.

int main() {
  char valor[5];
  char naipe[5];

  int c;

  int i = 0,j=0;
  do {
    c = getchar();
    if (((c > '0') && (c < '9')) || (c == 'K') || (c == 'Q') || (c == 'J') ||
        (c == 'A') || (c == 'T')) {
      valor[j] = c;
      j++;
      continue;
    }
    if ((c > 'A') && (c < 'Z')) {
      naipe[i] = c;
      i++;
    }
  } while (c != '\n');
    valor[j] = '\0';//terminate first then print.
    printf("%ld", strlen(valor));
    naipe[i] = '\0';

  return 0;
}
hanie
  • 1,863
  • 3
  • 9
  • 19