0

I want to extract last 3 digit of a string suppose like :

char a[100][100] = ["17BIT0111" , "17BIT0222", ... n];

and I want to take last three digits and store in different array like

int b[100] =[111 , 222 , ... n];

I took reference from this but I wan't it without using pointer or a linked list. As I am gonna use it for comparing stack. C program to extract different substrings from array of strings

Vivank
  • 156
  • 1
  • 14
  • you can't do anything without pointers - you need to go over your string somehow. But you don't need to dynamically allocate memory since your array sizes are hardcoded – Ora Dec 14 '17 at 16:13
  • @Ora by minimal use of pointer ? Or can't we extract here by performing nested loop or substring ? – Vivank Dec 14 '17 at 16:15

1 Answers1

1

Something like this:

for (int i = 0; i < 100; ++i)
{
  unsigned int value = 0;
  sscanf(a[i], "17BIT%u", &value);
  b[i] = (int) (value % 1000);
}

This doesn't check the return value of sscanf(), instead defaulting the value to 0 in case conversion fails.

This will convert a larger integer, so the % 1000 was added to make sure only the last three digits really matter in the conversion. The unsigned is simply to disallow embedded dashes in the string, which makes sense to me in cases like these.

unwind
  • 391,730
  • 64
  • 469
  • 606