×
FREE ASSISTANCE FOR THE INQUISITIVE PEOPLE
Tutorial Topics
X
softetechnologies
Differentiate between do-while and while statements WBUT MCA 2012 Reverse the digits of an integer - WBUT MAKAUT MCA 2012
C function to reverse a string and find the smallest divisor of an integer - WBUT MAKAUT MCA 2012 - C Language
2212    Arnab De    10/04/2020

Write a C function to reverse a string. Write a program in C which prints the smallest divisor of an integer, for example, the smallest divisor of 77 is 7.

WBUT / MAKAUT MCA 2012

softetechnologies

Reverse a string

We can reverse a string in two different ways- (i) Using another string (ii) without using second string. As per question here no need to write the main() function.

  • Using another string: Read the given string from the end and copy the characters in another string variable. After coping the entire variable a null (‘\0’) character place at last position of the new string to terminate it.
    		void reverse_string()
    		{
    			char str[40],strrev[40];
    			int len,i,j=0;
    			printf("Enter a String");
    			gets(str);
    			len=strlen(str);
    			for(i=len-1;i>=0;i--)
    			{
    				strrev[j++]=str[i];
    			}
    			strrev[j]='\0';
    			puts(strrev);
    			getch();
    		}
    		
  • softetechnologies
  • Without using second string: This method is much more accurate than above. Because according to the question, we are instructed to reverse the existing string, but in the first method, the original string is not changed.

    In this method, we interchange the first characters with the last character, after then, interchange the second character with the second last character and so on up to reach at the mid of the given string.

    			void reverse_string()
    			{
    				char str[40],temp;
    				int len,i;
    				printf("Enter a String");
    				gets(str);
    				len=strlen(str);
    				for(i=0;i<len;i++)
    				{
    					if(if<=(len/2))
    					{
    						temp=str[i];
    						str[i]=str[len-1-i];
    						str[len-1-i]=temp;
    					}
    				}
    				puts(str);
    				getch();
    			}
    			

Smallest Divisor

Here we start the divide the given number by 2. If it is not divided by 2 then divide it by 3 and so on. By which no the given number is divisible is known as the smallest divisor of the given number. As per question here we must write the main() function.

void main()
{
	int no,sd=2;
	printf("Enter a number");
	scanf("%d",&no);
	while(no%sd!=0)
	{
		sd++;
	}
	printf("The smallest divisor of %d is %d",no,sd);
	getch();
}
Differentiate between do-while and while statements WBUT MCA 2012 Reverse the digits of an integer - WBUT MAKAUT MCA 2012
softetechnologies
Author Details
Arnab De
I have over 16 years of experience working as an IT professional, ranging from teaching at my own institute to being a computer faculty at different leading institute across Kolkata. I also work as a web developer and designer, having worked for renowned companies and brand. Through tutorialathome, I wish to share my years of knowledge with the readers.
Enter New Comment
Comment History
No Comment Found Yet.
Swami Vivekananda
Education is not the amount of information that is put into your brain and runs riot there, undigest
Swami Vivekananda
598
78.5
Today So Far
Total View (Lakh)
softetechnologies
26/05/2018     53634
25/06/2018     44652
01/01/2018     43347
28/06/2017     40988
02/08/2017     39938
01/08/2017     34003
06/07/2017     33838
15/05/2017     33082
11/09/2018     29681
14/07/2017     29487
softetechnologies