LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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
 
LinkBack Search this Thread
Old 03-19-2010, 02:32 AM   #1
sokha
Member
 
Registered: Sep 2008
Posts: 46

Rep: Reputation: 15
C - How to check if an input char contain numbers


Hello,

I would like to create a small C tool. I encounter a problem of how to make a function to check an input chars contains numbers (started from the second element).

Some samples:

char *mychar= "a3547"; (The result of function checking this is true)
char *another_char = "t6548"; (The result of function checking this is true)
char *next_char = "appl3"; (The result of function checking this is false)
char *new_char = "b1aa3"; (The result of function checking this is false)

Thanks

Last edited by sokha; 03-19-2010 at 02:33 AM.
 
Old 03-19-2010, 02:35 AM   #2
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: washington U.S.
Distribution: Damn Small Linux, KateOs, M$ Ickdows Vista, My own OS
Posts: 2,094

Rep: Reputation: 104Reputation: 104

OOPS that was suppose to be && not ||.

Code:
int number_inside(char *ip){
 for(int p = 1;p < strlen(ip);p++){
  if(ip[p] >= '0' && ip[p] <= '9'){return 1;} 
 }
 return 0;
}

Last edited by smeezekitty; 03-19-2010 at 12:49 PM.
 
Old 03-19-2010, 07:35 AM   #3
johnsfine
Senior Member
 
Registered: Dec 2007
Distribution: Mepis, Centos
Posts: 4,012

Rep: Reputation: 731Reputation: 731Reputation: 731Reputation: 731Reputation: 731Reputation: 731Reputation: 731
Quote:
Originally Posted by sokha View Post
how to make a function to check an input chars contains numbers (started from the second element).
Your examples did clarify that ambiguous specification. But you should learn to specify behavior with less ambiguity. Such as

Return true if every character, from the second through the last inclusive, is a digit.
Return false otherwise.

Quote:
Originally Posted by smeezekitty View Post
(ip[p] >= '0' || ip[p] <= '9')
The overall logic of your answer would be wrong even if the test (what I quoted) were correct. Maybe you misunderstood the ambiguous description of the problem. (Maybe you answered before the first post was edited and maybe then it was even less clear than it is now).

But look at your test itself. I hope you can see it doesn't test anything, it is always true.

Quote:
Originally Posted by sokha View Post
I encounter a problem
Is this a homework assignment?

If you know how to program in C (even just a little) this function should be trivial.

If this is homework, we should not be giving you a final answer, just explanations and ideas.

If this isn't homework, you're trying to do something you're not ready for yet. Go through a C tutorial and learn the language a little before trying to program in it.

Smeezekitty apparently got careless in coding and also misunderstood your problem specification. But at least he demonstrated the basic concepts required for the task.

Last edited by johnsfine; 03-19-2010 at 07:52 AM.
 
Old 03-19-2010, 09:28 AM   #4
ForzaItalia2006
Member
 
Registered: Dec 2009
Location: Walldorf, Germany
Distribution: (X)Ubuntu, Arch, Gentoo
Posts: 205

Rep: Reputation: 67
Hey,

John is right. Just as a hint to simplify your code. Take a look at <ctype.h>, especially the function

int isdigit(int c);

- Andi -
 
Old 03-23-2010, 06:19 AM   #5
sokha
Member
 
Registered: Sep 2008
Posts: 46

Original Poster
Rep: Reputation: 15
Thank you all. I could figure out of how to do it now. I am not a student nor a programmer. I am doing a little coding for my friend. It is about IRC things.

I think I should go and learn C from beginner. I may need help later on again.
 
  


Reply

Tags
char, checking


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Scripting: How to verifiy if input matches a range of numbers? klss Linux - Software 3 03-11-2009 04:57 AM
Input in integers, users writes a char, how to catch exception? raskol Programming 4 03-30-2008 12:28 PM
C how to check if the input of scanf is an integer as opposed to a char? sycamorex Programming 1 02-07-2008 04:00 PM
Wow..Check out the numbers.... cwwilson721 Slackware 19 01-05-2006 11:16 PM
count char on input centr0 Programming 7 07-06-2003 12:03 PM


All times are GMT -5. The time now is 03:07 PM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration