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

Write a program that prompts the user to enter a date in the format mm/dd/yyyy where...
Write a program that prompts the user to enter a date in the format mm/dd/yyyy where mm is the month , dd is the day and yyyy is a 4 digit year Check if the date is valid by seeing month is between 1 and 12, day is between 1 to 31 and year is between 1800 to 3000. Also check that if month is 2, day is between 1 to 29 If the date is valid then display the...
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...
Write a c++ program that ask a user to enter his name and birthday (YYYY/MM/DD). If...
Write a c++ program that ask a user to enter his name and birthday (YYYY/MM/DD). If the age is greater than 21 print "welcome," and if the age is less than 21 print "sorry." Use input validation to make sure the birthdate was entered correctly.
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...
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...
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
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT