LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-27-2011, 11:40 PM   #1
ellainexx
LQ Newbie
 
Registered: Aug 2011
Posts: 1

Rep: Reputation: Disabled
Unhappy string problem( programming C )


hello!
I've asked all of my friends about this problem but nobody knows it.
okay here's the problem, we need to output this:


Enter the date today: July 5 2008

Today is 7 5 08


how can I do that in programming.
Our teacher said, we're gonna use String , strcmp in this problem.. I dont really understand string ..

Hope someone could help!
thanks Godbless.
 
Old 08-28-2011, 12:34 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
You need to tell us what exactly it is you don't understand, as well as what you've tried already. If you haven't tried anything, at least tell us how you think you need to go about solving the problem. That way, we can help you with each step and help you learn. It's better than someone just posting the solution for you.

Last edited by Nylex; 08-28-2011 at 12:49 AM.
 
Old 08-28-2011, 01:03 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Well based on so little information and effort on your behalf i would simply say that the last 2 sets of numbers (5 and 08) are obvious and if you had a list of months that are somehow
associated with numbers that you could 'compare' the first 'string' and get the required number.
 
Old 08-28-2011, 06:59 AM   #4
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
C does not really know strings; only character arrays.

Assuming the user of the program needs to enter the date

Code:
printf("Enter the date today: ")
To read the user data
Code:
gets(yourbuffer)
or better
Code:
fgets(yourbuffer,sizeofyourbuffer,0)
To split the user date, use strchr or strtok
To compare
Code:
int month;
if(strcmp(month_from_userdata,"July")==0)
{
    month=7;
}
Hope this gets you on the way.
 
Old 08-28-2011, 11:53 PM   #5
murugesan
Member
 
Registered: May 2003
Location: Bangalore ,Karnataka, India, Asia, Earth, Solar system, milky way galaxy, black hole
Distribution: murugesan openssl
Posts: 181

Rep: Reputation: 29
Code:
int month = 0 ;
Detecting The Use Of Uninitialized Variables
http://www.cprogramming.com/debugging/valgrind.html




always check "==0" at the beginning of the condition like:
Code:
	if( 0 == strcmp(month_from_userdata,"July") )
	{
	    month=7;
	}
or
Code:
	if( !strcmp(month_from_userdata,"July") )
	{
	    month=7;
	}



Code:
fgets(yourbuffer,sizeofyourbuffer,0)
/* Check here for return value (handling errors) */
needs to be
Code:
fgets(yourbuffer,sizeofyourbuffer,stdin); /* like on Linux/HP UX */
as the last parameter for stream cannot be 0 which will create
1. core file ("ulimit -c unlimited" before executing a.out)
2. a.exe.stackdump on cygwin

Code:
gets(yourbuffer)
/* Check here for return value (handling errors) */

Last edited by murugesan; 08-29-2011 at 02:24 AM. Reason: Edited to include the url
 
Old 08-29-2011, 02:00 AM   #6
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
Thanks for correcting the fgets; was too lazy to check it out.

It's after all just sample code to show the steps hence no error checking whatsoever.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
serial port programming string problem utkarshrawat Programming 2 11-24-2010 02:21 PM
Programming in BASH - Parsing a String Mistro116@yahoo.com Programming 9 06-18-2008 06:43 PM
how to reverse a string in C programming egoleo Programming 12 04-05-2004 05:45 AM
C programming assigning an integer value to a string Linh Programming 4 06-22-2003 07:02 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration