Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I'm Newbie in C 1

Status
Not open for further replies.

zaq888

MIS
Jun 2, 2003
46
0
0
MY
Alooo Guys...

i'm beginner in c programming.
How c can reads sequence of digit from keyboard
using scanf() then print them until standart output in words...

eg: input from keyboard : 123

then printf output one,two,three

please help....

I'm Keep Studying.... please show the way...
Not Good in English
 

Code:
int main(void) {
int i = 0;
char str[25];
    printf("Please enter your string: ");
    scanf("%s",&str);
    
    while (i < strlen(str)) {
           printf(&quot;%c&quot;, str[i]);
           system(&quot;sleep 1&quot;);
           i++;
    }
return 0;
}

Of course you may need to write your own sleep for microshaft.
 
Thanks guy

I'm Keep Studying.... please show the way...
Not Good in English
 
zaq
iam still unclear about 1 thing..
how do u want the output to be ?

if input is 123
1) output is one two three &quot;&quot;or &quot;&quot;
2) 1 2 3 character by character
b'coz marsd code will give the output the second way!

There is 1 more correction in mars code

scanf(&quot;%s&quot;,&str); // the & is not reqd. since str itself
//is an array, ergo address!

i'll jus post the code for the first way shortly!
 
oh yup the code.
zaq .. kindly note that I have just made modifications to the code posted by marsd


#include <stdio.h>
#include <ctype.h>

int main(void) {
int i = 0;
char str[25];
char *a[10] ={&quot;zero&quot;, &quot;one&quot;,&quot;two&quot;,&quot;three&quot;,&quot;four&quot;,&quot;five&quot;,&quot;six&quot;,&quot;seven&quot;,&quot;eight&quot;,&quot;nine&quot;};
int invalid = 0;


printf(&quot;Please enter your string: &quot;);
scanf(&quot;%s&quot;,str);

for (i = 0 ; str ; ++i)
{
if (isdigit(str))
printf(&quot;%s\n&quot;,a[ (str - '0' ) ]);
else
{
invalid = 1;
break;
}
}

if (invalid)
printf(&quot;error in inputted string \n&quot;);

return 0;
}



Its a very rudimentary one , but nevertheless I guess its useful in capturing the essence..

I guess u can implement the same using a switch case..

i hope this helps!
-vs

 
huh! HOW DID THE ITALICS COME ???

THE CODE AGAIN.... SEE THIS

Code:
#include <stdio.h>
#include <ctype.h>

int main(void) {
int i = 0;
char str[25];
char *a[10] ={&quot;zero&quot;, &quot;one&quot;,&quot;two&quot;,&quot;three&quot;,&quot;four&quot;,&quot;five&quot;,&quot;six&quot;,&quot;seven&quot;,&quot;eight&quot;,&quot;nine&quot;};    
 int invalid = 0;
 
    
    printf(&quot;Please enter your string: &quot;);
    scanf(&quot;%s&quot;,str);
    
   for (i = 0 ; str[i] ; ++i)
   {
		 if (isdigit(str[i]))
   		  	printf(&quot;%s\n&quot;,a[ (str[i] - '0' ) ]);
  		else
  			{
 				invalid = 1;
  				break; 	
  			}	 
   }
   
   if (invalid)
      	printf(&quot;error in inputted string \n&quot;);

	return 0;
}


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top