# convert int month to string month

• (2 Pages)
• 1
• 2

## 15 Replies - 14009 Views - Last Post: 06 March 2013 - 05:43 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=314425&amp;s=11d925aefed268bdfa2b607a67d4061b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Gagzzy

Reputation: 0
• Posts: 27
• Joined: 22-April 12

# convert int month to string month

Posted 05 March 2013 - 01:18 PM

hi i have a program to sort out dates, i have been able to convert the date to integer but cant work out how to get it back to string how can i do that?

this is my month array and function to convert the given month to int
```char* allMonths[]= {"January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"};

int monthToInt(char tempMonth[])
{
for(int i = 0; i<12; i++)
{
if (tempMonth == allMonths[i])
{
return i;
}
}
return 0;
}

```

Is This A Good Question/Topic? 0

## Replies To: convert int month to string month

### #2 jimblumberg

Reputation: 5391
• Posts: 16,812
• Joined: 25-December 09

## Re: convert int month to string month

Posted 05 March 2013 - 01:25 PM

First you can't use the comparison operator== with C-strings, you will need to use something like strcmp().

As for your actual question what have you tried?

Jim

### #3 Martyr2

• Programming Theoretician

Reputation: 5150
• Posts: 13,837
• Joined: 18-April 07

## Re: convert int month to string month

Posted 05 March 2013 - 01:26 PM

Just use the index to access your array...

```char* intToMonth(int index) {
char* allMonths[]= {"January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"};
return allMonths[index];
}

```

That should do the trick for you. Of course make sure it is in range of 0 through 11... you can do that with an if statement or use modulus to keep it in range.

I will leave that part to you.

This post has been edited by Martyr2: 05 March 2013 - 01:27 PM

### #4 Gagzzy

Reputation: 0
• Posts: 27
• Joined: 22-April 12

## Re: convert int month to string month

Posted 05 March 2013 - 01:40 PM

Martyr2, on 05 March 2013 - 01:26 PM, said:

Just use the index to access your array...

```char* intToMonth(int index) {
char* allMonths[]= {"January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"};
return allMonths[index];
}

```

That should do the trick for you. Of course make sure it is in range of 0 through 11... you can do that with an if statement or use modulus to keep it in range.

I will leave that part to you. />

thanks that seems to print out dates just not the ones that get entered how do i call that correctly?

### #5 Martyr2

• Programming Theoretician

Reputation: 5150
• Posts: 13,837
• Joined: 18-April 07

## Re: convert int month to string month

Posted 05 March 2013 - 02:01 PM

Uhhh well if they enter a date like 3/5/2013 (March 5th of 2013) then you would take off the 3, subtract 1 (since you based your array on zero index) and pass it to the function...

```cout << "Month of the date you entered is: " << intToMonth(month - 1) << endl;

```

It is the opposite of your first function where you went from month to int, the function I showed is the int back to month.

### #6 Gagzzy

Reputation: 0
• Posts: 27
• Joined: 22-April 12

## Re: convert int month to string month

Posted 05 March 2013 - 06:37 PM

thanks i understand that, by the looks of it the problem lies with my inttomonth function it doesnt seem to be converting the strings to int would you know why ?

ive tried this way
```int monthToInt(char tempMonth[])
{
for(int i = 0; i < 11; i++)
{
if (tempMonth == allMonths[i])
{
return i;
}
}
}

```

and this way
```int monthToInt(char tempMonth[])
{
for(int i = 0; i < 11; i++)
{
if (strcmp(tempMonth,allMonths[i]) == 0)
{
return i;
}
}
}

```

messed around with both but still value stays at 0 meaning every month printed is january

### #7 #define

• Duke of Err

Reputation: 1853
• Posts: 6,671
• Joined: 19-February 09

## Re: convert int month to string month

Posted 05 March 2013 - 06:45 PM

Maybe the month is not being found. Try returning -1 at the end of the function.

### #8 jjl

• Engineer

Reputation: 1270
• Posts: 4,998
• Joined: 09-June 09

## Re: convert int month to string month

Posted 05 March 2013 - 06:48 PM

Can you post how you are calling the function? You might not be null terminating your input string.

### #9 Gagzzy

Reputation: 0
• Posts: 27
• Joined: 22-April 12

## Re: convert int month to string month

Posted 06 March 2013 - 01:32 AM

using a for loop with temp variable

```for(int i = 0; i < n; i++)
{
scanf("%s %d %d", & tempMonth[i], & date[i].day, & date[i].year);
date[i].month = monthToInt(tempMonth);
}

```

• D.I.C Lover

Reputation: 331
• Posts: 1,168
• Joined: 01-April 11

## Re: convert int month to string month

Posted 06 March 2013 - 02:36 AM

The problem is already done for you - to convert a month back to it's int, you simply check each of the strings in *allMonths[].

But in C, you don't use things like if(string1 == string2), instead you use if(strcmp(string1,string2) == 0)

Here's an example using your code:

```#include <stdio.h>
#include <string.h>  //for strcmp()

int monthToInt(char tempMonth[],char *allMonths[]);

int main(void) {

char* allMonths[]= {"January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"};

char tempMonth[10]; //was 9, thanks for noticing JackofAllTrades!
int i;

for(i=0;i<12;i++) {
printf("%s is number %d\n",allMonths[i],i+1);
}
printf("\n\n");
while(tempMonth[0] != '-1') {
printf("Enter a month to see it's number [-1 to Quit]: ");
fflush(stdout);
scanf("%s",tempMonth);

i=monthToInt(tempMonth,allMonths);
if(i>=0)
printf("That months number is: %d\n",i+1);

}
return 0;
}

int monthToInt(char tempMonth[], char *allMonths[])
{
for(int i = 0; i<12; i++)
{
if (strcmp(tempMonth,allMonths[i])==0)
{
return i;
}
}
return 0;
}

```

This post has been edited by Adak: 06 March 2013 - 04:49 AM

### #11 Gagzzy

Reputation: 0
• Posts: 27
• Joined: 22-April 12

## Re: convert int month to string month

Posted 06 March 2013 - 03:41 AM

its the beginning finding the int value of the month ive used strcmp,

the input should be
8
January 1 01
January 1 00
July 17 12
September 10 12
July 1 00
August 25 06
May 27 08
October 1 03

output
January 1 00
July 1 00
January 1 01
October 1 03
August 25 06
May 27 08
July 17 12
September 10 12

but am getting
January 1 0
January 1 0
February 1 1
January 1 3
January 25 6
January 27 8
January 10 12
January 17 12

• Saucy!

Reputation: 6246
• Posts: 24,014
• Joined: 23-August 08

## Re: convert int month to string month

Posted 06 March 2013 - 03:56 AM

```char tempMonth[9];
```

That is not enough space to hold "September".

And what you've provided for code is nothing like what you've provided for expected input/output.

### #13 Gagzzy

Reputation: 0
• Posts: 27
• Joined: 22-April 12

## Re: convert int month to string month

Posted 06 March 2013 - 04:09 AM

JackOfAllTrades, on 06 March 2013 - 03:56 AM, said:

```char tempMonth[9];
```

That is not enough space to hold "September".

And what you've provided for code is nothing like what you've provided for expected input/output.

haha no worries heres the program in full

```#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct dates
{
int day;
int month;
int year;
};

char* allMonths[]= {"January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"};

int monthToInt(char tempMonth[])
{
for(int i = 0; i < 12; i++)
{
if (strcmp(tempMonth, allMonths[i]))
{
return i;
}
}
}

char* monthToString(int index)
{
return allMonths[index];
}

int cmp(const void* p1, const void* p2)
{
if  (((struct dates*)p1)->year < ((struct dates*)p2)->year)
{
return -1;
}
else if(((struct dates*)p1)->year > ((struct dates*)p2)->year)
{
return 1;
}

if  (((struct dates*)p1)->month < ((struct dates*)p2)->month)
{
return -1;
}
else if(((struct dates*)p1)->month > ((struct dates*)p2)->month)
{
return 1;
}

if  (((struct dates*)p1)->day < ((struct dates*)p2)->day)
{
return -1;
}
else if(((struct dates*)p1)->day > ((struct dates*)p2)->day)
{
return 1;
}

else return 0;
}

int main() {

int n;
char tempMonth[100];

scanf("%d", & n);

struct dates *date;
date = (struct dates*)malloc(sizeof(struct dates) *n);

for(int i = 0; i < n; i++)
{
scanf("%s %d %d", & tempMonth[i], & date[i].day, & date[i].year);
date[i].month = monthToInt(tempMonth);
}

qsort(date, n, sizeof(struct dates), cmp);

for(int i = 0; i < n; i++)
{
printf("%s %d %d\n", monthToString(date[i].month), date[i].day, date[i].year);
}
}

```

• Saucy!

Reputation: 6246
• Posts: 24,014
• Joined: 23-August 08

## Re: convert int month to string month

Posted 06 March 2013 - 04:21 AM

Holds one, single C string.
```char tempMonth[100];
```

To use it, you do not do this:
```scanf("%s %d %d", & tempMonth[i], & date[i].day, & date[i].year);
```

you do this:
```scanf("%s %d %d", tempMonth, & date[i].day, & date[i].year);
```

The name of an array is a pointer to the first element in the array.

If you're compiling this as C, you should not be casting the return value of malloc here:
```date = (struct dates*)malloc(sizeof(struct dates) *n);
```

It's unnecessary and can hide other issues.

### #15 Gagzzy

Reputation: 0
• Posts: 27
• Joined: 22-April 12

## Re: convert int month to string month

Posted 06 March 2013 - 05:00 AM

output added another February to the list (N)