logo
down
shadow

How to scan specific string format in C?


How to scan specific string format in C?

By : Prudvi Reddy
Date : October 20 2020, 06:10 AM
may help you . You can use strptime to parse the time, and then a combination of mktime and strftime to validate the date is actually valid.
Consider the following code:
code :
#define _XOPEN_SOURCE
#include <time.h>
#include <stdio.h>
#include <string.h>

int main(void)
{
    char birth_date[12], *p, *state="invalid";
    struct tm tv = {0};
    fprintf(stdout, "Enter date: yyyy-mm-dd: ");
    if (fgets(birth_date, sizeof(birth_date), stdin) != NULL)
    {
        if ((p = strchr(birth_date, '\n')) != NULL)
            *p = 0;  /* Remove the newline left by fgets */

        if ((p = strptime(birth_date, "%Y-%m-%d", &tv)) != NULL)
        {
            char b[12];
            mktime(&tv); 
            if ((strftime(b, sizeof(b), "%Y-%m-%d", &tv) > 0) &&
                 strcmp(b, birth_date) == 0)
            {
                state = "valid";
            }
        }
        fprintf(stdout, "Date %s is %s\n", birth_date, state);
    }
    return 0;
}
 Enter date: yyyy-mm-dd: 2019-05-11
 Date 2019-05-11 is valid

 Enter date: yyyy-mm-dd: foo
 Date foo is invalid

 Enter date: yyyy-mm-dd: 2018-02-29
 Date 2018-02-29 is invalid

 Enter date: yyyy-mm-dd: 2016-02-29
 Date 2016-02-29 is valid


Share : facebook icon twitter icon
How static scan all C# code for string format argument errors

How static scan all C# code for string format argument errors


By : Tanveer Ahmed
Date : March 29 2020, 07:55 AM
I wish this helpful for you We're using VS2012. The code analysis tool built into it helped us identify all of those types of issues.
How to scan a string for specific terms

How to scan a string for specific terms


By : SQLism
Date : March 29 2020, 07:55 AM
will be helpful for those in need I'm trying to scan user input text for specific words and then, when those words occur, print them to the console. , You need strstr function from string.h
code :
const char * strstr ( const char * str1, const char * str2 );
#include <stdio.h>
#include <string.h>

#define MAX_STR_LEN 120

int main(){
    char cArray[MAX_STR_LEN] = "example";  // string to be searched in the input string
    char cInput[MAX_STR_LEN] = ""; // input string
    char cOutput[MAX_STR_LEN] = ""; // buffer for found string

    printf("\nType your message:\n");
    for (int y=0; y<1; y++){     // this loop from original example looks strange, but it works
        fgets(cInput, MAX_STR_LEN, stdin);
    }
    // search in the input string
    char * ptr;
    if( ( ptr=strstr(cInput, cArray) ) != NULL)
    {
        //copy the string to cOutput
        strncpy(cOutput, ptr, strlen(cArray));
        // output the found string
        printf("String that was found: \n%s\n", cOutput);
    }
    else
    {
        printf("String was not found in the input!\n");
    }
}
    // search in the input string
    char * ptr = cInput;
    while( ( ptr=strstr(ptr, cArray) ) != NULL)
    {
        //copy the string to cOutput
        strncpy(cOutput, ptr, strlen(cArray));
        // output the found string
        printf("String \"%s\" was found at position %d\n", cOutput, (int)(ptr - cInput + 1));
        // find next string
        ptr++;
    }
How to scan time in hh:mm format as string and get the value of hh and min in integer?

How to scan time in hh:mm format as string and get the value of hh and min in integer?


By : Mgbada Chike
Date : March 29 2020, 07:55 AM
hope this fix your issue , Try this (Assuming input format HH:MM):
code :
String s = sc.next(); //instead of nextLine()
int hh = Integer.parseInt(s.substring(0,2));
int mm = Integer.parseInt(s.substring(3));
String s = sc.next(); //instead of nextLine()
String arr[] = s.split(":");
int hh = Integer.parseInt(arr[0]);
int mm = Integer.parseInt(arr[1]);
segmentation fault while using format string to scan input

segmentation fault while using format string to scan input


By : Mahesh Nironi
Date : March 29 2020, 07:55 AM
it should still fix some issue consider the code and its result:
Scan DOM and return or manipulate elements that have specific attributes prepended with specific string

Scan DOM and return or manipulate elements that have specific attributes prepended with specific string


By : Ram Prasath V
Date : March 29 2020, 07:55 AM
around this issue I am trying to figure out how I can get a list of elements from the DOM (top to bottom) containing specific attributes that are prefixed with a given string lets say for the sake of example "monkey" is that string. If any exist. , You can try this way:
Related Posts Related Posts :
  • dynamic memory for 2D char array
  • C compiler producing lightweight executeables
  • Cannot Open Shared Object cygmpfr-1.dll
  • How to access a structure member in a function that get it as void* type?
  • Replace for loop with formula
  • how to find if stack increases upwards or downwards?
  • Why does my homespun sizeof operator need a char* cast?
  • Setting exit status when creating core dump
  • Signal Handling in C
  • How to implement B+ Tree for file systems?
  • Stack storage at function call
  • Manipulate the Clipboard in C on Mac OS X
  • Problem with writing a hexadecimal string
  • Why does this code read all ' ' for the anything after the 4th character?
  • GUI for linux c
  • Where do I put all these function-like #defines, in C?
  • Padding in 24-bits rgb bitmap
  • how to acess and change variable of kernel space from user space
  • Can a conforming C implementation #define NULL to be something wacky
  • send() always interrupted by EPIPE
  • Socket Select with empty fd set
  • Coding self similar fractals in computer graphics using C
  • Forking with Pipes
  • sscanf + c99 not working on some platforms?
  • Problem with C function of type char pointer, can someone explain?
  • Macros in C.... please give the solution
  • Unix Piping using Fork and Dup
  • Any difference between these two while loops?
  • Hashtable implementation for C
  • Problems with Linked List in C
  • Stack Allocation in C
  • Random function stuck on one number
  • printf following zeros to a certain degree
  • Measuring CPU clocks consumed by a process
  • How is a CRC32 checksum calculated?
  • Tips on redefining a register bitfield in C
  • Converting a program from c to bash script
  • Static library not included in resulting LLVM executable
  • What is the meaning of "wild pointer" in C?
  • Reading DWORD from binary file
  • Efficient way to find divisibility
  • Whats the fastest, portable way to hash pointers we know are pointer aligned to a fixed size int?
  • Why does my summation program behaves weird?
  • How to determine values saved on the stack?
  • Thread scheduling C
  • C - converting to 2s complement
  • wireless networks c program
  • From where does the uboot get the information about flash, RTC, RAM in uboot/kernel development
  • printf behaviour in C
  • Measure size and way-order of L1 and L2 caches
  • Efficient copy of entire directory
  • Efficient way to create/unpack large bitfields in C?
  • getnameinfo prototype asks for sockaddr not sockaddr_in?
  • Linked List Sorting with Strings In C
  • Typecasting a floating value or using the math.h floor* functions?
  • Problem implementing sorting algorithm in C with an array of structs
  • How to copy a ram_base file to disk efficiently
  • How to fix casting a pointer to an integer?
  • How to round floating point numbers to the nearest integer in C?
  • PIC C - Sending 200 values over USB, but it only sends 25 or so of them
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk