-1

I am new in C.

This is my code

#include <iostream>
#include <string.h>
using namespace std;

int main() {
char time[20];
       scanf("%s",time);

       // command and "hello" can be less than, equal or greater than!
       // thus, strcmp return 3 possible values
       if (strcmp(time, "PM") == 0)
       {
          printf("It's PM \n");
       }


    return 0;
}

Suppose I have an input 12:13:14PM

I want to find out if it is am or PM. But the above code only finds out if the whole char array is "PM" or not. I have seen other post by I could not understand them.

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
Adnan Sabbir
  • 181
  • 1
  • 13
  • https://stackoverflow.com/questions/13195353/how-to-find-substring-from-string – WhozCraig May 07 '17 at 19:24
  • Basically what you need is to parse your time input using `strptime()`, after you may easily to tell what was that, including proper handle of time zone (in whatever locale you run your process). – 0andriy May 07 '17 at 19:28
  • That's not C. No idea why you use C **coding style** is C++, though. – too honest for this site May 07 '17 at 20:16
  • *I have seen other post by I could not understand them.* -- Please point to the examples you say you couldn't understand. If you're talking about calling library routines, then they are not "understandable" because parsing date and time information is not trivial. Yes, right now you want "PM" and "AM", but then in a comment in the answer section, you want to detect hours as an integer. None of this is trivial doing this by hand, and library facilities are used instead. – PaulMcKenzie May 07 '17 at 20:23

2 Answers2

5

strcmp checks the whole string for equality, to check for a substring use strstr:

if (strstr(time, "PM") != NULL)
  printf("It\'s PM \n");

Side note, only reserving 20 char's for your input might be troublesome.


Also, your code looks like C++ instead of C, if this is the case use cin and std::string instead:

std::string time;
std::cin >> time;
if(time.find("PM") != std::string::npos)
  std::cout << "It\'s PM \n";
Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
2

You can use the function `char * strstr ( const char *str1, const char *str2);' - this functions a pointer to the first occurrence in str1 of the entire sequence of characters specified in str2, or a null pointer if the sequence is not present in str1.

if (strstr(time,"PM")!= NULL)
     printf("It's PM \n");

You can use strcmp and strstr both to ensure that PM occurs only at the end.

 if (strcmp(strstr(time,"PM"),"PM")== 0)
     printf("It's PM \n");

Also your code gives compilation error because you have not included stdio.h and using scanf and printf functions. So either include 'stdio.h' or use cin and cout for I/O operations.

GAURANG VYAS
  • 689
  • 5
  • 16
  • Thank you, it worked. :) Can you also tell me how to convert time[0] which is here "1" if the input is 12:13:14PM I want to convert it to int. Or you can say I want the 12 to be int – Adnan Sabbir May 07 '17 at 19:36
  • @AdnanSabbir, see my comment to your post (and this answer is even worse than one from Gill. – 0andriy May 07 '17 at 19:37
  • @0andriy I read your comment on his question. His requirements are quite simple and he simply needs to check the presence of a "PM " in the string. Specifically the questions is targeted for him to explore new string functions in C. So, I think that the answer provided by Gill and me are appropraite. If you find the existing answers irrelevant or inappropraite then you can post your own answer. – GAURANG VYAS May 07 '17 at 19:43
  • @Adnan If you want to convert the hour part of time to int - read the string character by character till you encounter ':'. While you are reading a character convert each character read to int by doiing this - (char - '0'). Now maintain a variable say hour initialised to 0. and update it - hour = hour*10 + (char - '0') as you read the character. – GAURANG VYAS May 07 '17 at 19:50
  • 1
    @GAURANGVYAS -- All that leads to is bad code that will be easily broken by either bugs or corner cases the OP didn't expect. Better to use built-in libraries. – PaulMcKenzie May 07 '17 at 20:25