Question

In: Computer Science

How to tokenize a string date of format        dd/mm/yyyy into day,month and year without using built...

How to tokenize a string date of format        dd/mm/yyyy into day,month and year without using built in function of strok() or any other built in function in C++ (without classes).
Kindly help Please .

Solutions

Expert Solution

#include <iostream>

using namespace std;

int intValue(string s) {
        int val = 0;

        for(int i=0; i<s.length(); i++) {
                val = val * 10 + (s[i] - '0');
        }

        return val;
}

int main() {

        string s = "21/02/2018";

        // We will be creating the substrings of the string
        // using / character..
        // Substring function takes the start index and the 
        // length of the string to be taken

        int m, y, d;
        d = intValue(s.substr(0, 2));
        m = intValue(s.substr(3, 2));
        y = intValue(s.substr(6, 4));
        
        cout << "Day: " << d << endl;
        cout << "Month: " << m << endl;
        cout << "Year: " << y << endl;

}
**************************************************
Answered without any built in function for tokenization..

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

Date - month: int - day: int - year: int +Date() +Date(month: int, day: int, year:...
Date - month: int - day: int - year: int +Date() +Date(month: int, day: int, year: int) +setDate(month: int, day: int, year: int): void -setDay(day: int): void -setMonth(month: int): void -setYear(year: int): void +getMonth():int +getDay():int +getYear():int +isLeapYear(): boolean +determineSeason(): string +printDate():void Create the class Constructor with no arguments sets the date to be January 1, 1900 Constructor with arguments CALLS THE SET FUNCTIONS to set the Month, then set the Year, and then set the Day - IN THAT ORDER...
Day of week Write a program that asks the user for a date (year, then month,...
Day of week Write a program that asks the user for a date (year, then month, then day, each entered as a number on a separate line), then calculates the day of the week that date falls on (according to the Gregorian calendar) and displays the day name. You may not use any date functions built into Python or its standard library, or any other functions that do this work for you. Hint: Reverend Zeller may be useful, but his...
Write a Java console application that reads a string for a date in the U.S. format...
Write a Java console application that reads a string for a date in the U.S. format MM/DD/YYYY and displays it in the European format DD.MM.YYYY  For example, if the input is 02/08/2017, your program should output 08.02.2017
Create a PHP class named "User" with the following private fields: name, birthdate in yyyy/mm/dd format,...
Create a PHP class named "User" with the following private fields: name, birthdate in yyyy/mm/dd format, age, and department. In the class, include getter and setter methods to get and set the values of those variables. Author a data entry webform using HTML text input boxes and a submit button. When the user clicks on the submit button, a "User" class is instantiated and the new User object's fields are populated. The HTML input boxes correspond to the field in...
How to create a divide function in (Dr Racket) programming language without using the built in...
How to create a divide function in (Dr Racket) programming language without using the built in function " / " ?
Create a "Date" class that contains: three private data members: month day year (I leave it...
Create a "Date" class that contains: three private data members: month day year (I leave it to you to decide the type) "setters" and "getters" for each of the data (6 functions in total) One advantage of a "setter" is that it can provide error checking. Add assert statements to the setter to enforce reasonable conditions. For example, day might be restricted to between 1 and 31 inclusive. one default constructor (no arguments) one constructor with three arguments: month, day,...
C++ CLASSES 1. Define a class date that has day, month and year data members. 2....
C++ CLASSES 1. Define a class date that has day, month and year data members. 2. Define a class reservation with following data members: - An integer counter that generates reservation numbers. - An integer as a reservation number. The counter is incremented each time a reservation object is created. This value will be assigned as the reservation number. - Number of beds. - Reservation date from class date. 3. Define a class rooms with data members: - Room number...
Write a c++ program that asks the user for a date (Month and Day only) and...
Write a c++ program that asks the user for a date (Month and Day only) and displays the season in this day.The program asks then the user if he needs to enter another date, if he answers with ’Y’ or ’y’, the programwill take another date from the user and displays the season in this day.The program will keep repeating this until the user enters a character different than ’Y’ and ’y’.•Fall starts in September 21st•Winter starts in December 21st•Spring...
For these of string functions, write the code for it in C++ or Python (without using...
For these of string functions, write the code for it in C++ or Python (without using any of thatlanguage's built-in functions) You may assume there is a function to convert Small string into the language string type and a function to convert your language's string type back to Small string type. 1. int [] searchA,ll(string in...str, string sub): returns an array of positions of sub in in...str or an one element array with -1 if sub doesn't exist in in...str
would the following SQL statement returns next sunday's date? SELECT NEXT_DAY(TO_DATE('13-01-2005','DD-MM-YYYY'),'SUN') As "Date"FROM DUAL; A)  True B)  False...
would the following SQL statement returns next sunday's date? SELECT NEXT_DAY(TO_DATE('13-01-2005','DD-MM-YYYY'),'SUN') As "Date"FROM DUAL; A)  True B)  False Question 4 options: True False Question 5 (2 points) 1.  Which of the given SQL statement generate the following result? Today's date Monday ,March 2008 A) SELECT TO_CHAR(sysdate,'day,Month yyyy') "Today's date" FROM DUAL; B) SELECT TO_CHAR(sysdate,'Day,Mon yyyy') "Today's date" FROM DUAL; C) SELECT TO_CHAR(sysdate, "Day,Month yyyy ") "Today's date" FROM DUAL; D) SELECT TO_CHAR(sysdate,'Day,Month yyyy') "Today's date" FROM DUAL; Question 7 (2 points) Which of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT