Category Archives: String Programs

C Program to Convert Numbers Into Words

Lets make a program for converting number into words. I em not talking about converting numbers into their ASCII values, what I mean is juss like convert 1234 to “One Two Three Four”. I found this program useful and a better one to boost your logical skills.

Have ah Look at the Program ►►

#include<stdio.h>
#include<conio.h>
 
void main()
{
   int num,n,i=0;
   char *ptr[1000];

   printf("\nEnter any Number : ");
   scanf("%d",&num);

   while(num) {

   n=num%10;
   num/=10;

   switch(n) {

   case 0 :
           ptr[i++]="zero";
           break;

   case 1 :
           ptr[i++]="One";
           break;

   case 2 :
           ptr[i++]="Two";
           break;

   case 3 :
           ptr[i++]="Three";
           break;

   case 4 :
           ptr[i++]="Four";
           break;

   case 5 :
           ptr[i++]="Five";
           break;

   case 6 :
           ptr[i++]="Six";
           break;

   case 7 :
           ptr[i++]="Seven";
           break;

   case 8 :
           ptr[i++]="Eight";
           break;

   case 9 :
           ptr[i++]="Nine";
           break;
} // While close brace

for(int j=(i-1);j>=0;j--)
  printf("%s\t",ptr[j]); 
 
getch();   
}

Explanation ►►

So what is done in the program is first we declared a string pointer (*ptr[1000]), for storing the words of number in the pointer and then print them in reverse order to get the right output. Then conditioned the while loop as while(num) it will execute till the number comes to zero.

After the user entered the number it will store in the variable num, now it will go through the while loop till it does not have the value of zero. In while loop the first statement (n=num%10) will take the last digit of the num and will assign to variable n then we use the statement num/=10; (num=num/10;) to remove the last number from the variable num. It;s like suppose user entered 1234 then by first statement 4 will be stored in n as %(modulus) operator always return the remainder and then the number will be divide by 10 to remove the last digit(4), after the second statement the num value will be 123 and so on.

Then when we got the value in n we go to the switch statement then the variable n is compared with the cases and the matched case will be execute. As the value of n is 4(suppose) the case 4 will be execute and the string “Four” will be stored in char pointer(ptr) following the ptr[i++]=”Four”; statement. We use i++ inside the dimension of the array pointer because after every string stored the value of i will be incremented to point the next location in the char pointer to store any other string. The i++ is the post increment that means after using the variable (assigning the value to the location) it will increment the value of i by 1.

This process will be done for all digits and once the number becomes zero, means there is no digit left, the while loop will be terminate and then we will simply print the words in reverse order(because the digit last in the number is taken first) to make right form of the words.

Here in for loop we assign j as j=i-1, because the last case that will execute will increment the value of i, that means the i will be pointing to the empty location of the char pointer. By taking i-1 it will point to the last location of the char pointer.

C Program to Compare Two Strings Through User-Defined Function

Strings are another good feature of C. They provide a great way to store characters, not in really, in actual C Program stores the ASCII Values of characters that’s why there is no any non-numeric data-type in C. All in hidden, access and manipulate through numeric values. In other words, Strings are arrays of characters.  

There are many functions for strings and to compare two strings there is also strcmp(str1,str2); function. Which if the two strings are equal gives the value 0(zero) and otherwise return the ASCII value difference between first non-matching characters.

Lets do it with some trick to eliminate the variables and shorten the size of the program.

Lets Do It with the User-define Program ►► Continue reading C Program to Compare Two Strings Through User-Defined Function