×
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
2282    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.
Sri Sri Ramakrishna Paramahamsa
All troubles come to an end when the ego dies
Sri Sri Ramakrishna Paramahamsa
2505
80.89
Today So Far
Total View (Lakh)
softetechnologies
26/05/2018     54139
25/06/2018     45134
01/01/2018     43682
28/06/2017     41217
02/08/2017     40232
01/08/2017     34282
06/07/2017     34077
15/05/2017     33325
11/09/2018     30338
14/07/2017     29844
softetechnologies