Thread: C program to find three-digital numbers in an array

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    5

    C program to find three-digital numbers in an array

    Hello there,

    I have a task that sounds like this: We have a .txt file which consists of numbers and some text, we need to create a program which reads a file and detects whether there is three-digital numbers or not (like 123;354;999 and etc)... And then the given result import to the text file. So I know how to do the last part (import results) and I know how to read a file and print everything out but what I do not understand how to check if there is such numbers. I mean when we read the text file we usually store information on an array, so how do we check if there is such elements in array? I'm really new to c programming and I usually find info on internet (youtube, read forums, examples and etc) but on this one I couldn't find any information that would help me, so I decided to ask here If someone could help me out, maybe just give a clear hint so that I could actually have a conception on how to do it. My english isn't very good sorry and please if you write a confusing answer just try to write it in as simple english as possible.

    Thank You.

  2. #2
    Banned
    Join Date
    Aug 2017
    Posts
    861
    sscanf and

    look into how to set up that function to scan for number between 0 - 9 in sets of however many you have to deal with, then put them into a different file

    using fgets with a buffer should help you as well. fgets gets chunks of data from then stream then use sscanf to look at the buffer with its specifiers to tell it to look for numbers.
    scanf, fscanf, sscanf, scanf_s, fscanf_s, sscanf_s - cppreference.com

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,465
    Please post a small sample of your input file.

  4. #4
    Registered User
    Join Date
    Sep 2017
    Posts
    5
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
        char textfile[99];
        FILE *ftxtpoint;
        if ((ftxtpoint = fopen("text.txt", "r")) == NULL)
        {
            printf("ERROR!");
            exit(1);
        }
        fscanf(ftxtpoint,"%[^\n]", textfile);
        printf("%s", textfile);
    
    
        // This is the part where I should put something to look for three-digital numbers
    
    
        fclose(ftxtpoint);
    
    
        return 0;
    }
    My text file looks like this:
    C program to find three-digital numbers in an array-pavadinimo-png

    The circled numbers should be the result of the program. Let's say in this situation it should write on the end: This .txt file has 3 three-digital numbers.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,465
    You may want to consider using fscanf() (with the "%s" specifier) in a loop to get the "words" then test the "word" to see if it starts with an alpha or a digit. The if it starts with a digit you can test the length to see if it is what you're looking for.

    Don't forget to use the proper limiting width parameter to avoid any possible array overruns.

    something like:

    Code:
    while(fscanf("%98s", textfile) == 1)
    {
        if(isdigit(textfile[0]))
           if(strlen(textfile) == 3)
               // Here it is do something.
    }

  6. #6
    Registered User
    Join Date
    Sep 2017
    Posts
    5
    WOW! That was soo simple! Can't believe it... I just edited your code a bit and it works flawlessly! A very very BIG thanks!

  7. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,728
    If you want a more general solution that would work for all cases, try this:
    Code:
    // When I say "d" I mean digit, and with "v" I mean anything but a digit
    while(fgets(textfile, 99, ftxtpoint) != NULL) {
        int strLen = strlen(textfile);
        int i;
    
        if (strLen > 3) {
            // Check if txtfile[0...3] is "dddv"
            // Check if txtfile[(strLen-4)...(strLen-1)] is "vddd"
    
            for (i = 2; i < strLen-2; ++i) {
                // Check if txtfile[(i-2)...(i+2)] is "vdddv"
            }
        } else if (strLen == 3) {
            // Check if txtfile[0...2] is "ddd"
        }
    }
    Last edited by GReaper; 12-08-2017 at 03:30 PM.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to find out how much numbers are in array
    By Jakub Elia in forum C Programming
    Replies: 2
    Last Post: 03-11-2015, 11:45 AM
  2. Replies: 7
    Last Post: 04-07-2013, 03:45 PM
  3. how to find numbers in a char array.
    By amzc in forum C Programming
    Replies: 5
    Last Post: 10-14-2012, 10:33 PM
  4. How to find the smallest 5 numbers in an array?
    By atac in forum C Programming
    Replies: 4
    Last Post: 08-28-2012, 02:27 AM

Tags for this Thread