C program for swapping of two strings

  • C program for swapping of two strings.
  • C program to swap two strings without using library functions.
  • Now we will swap two strings without using library functions and by using arrays in java.
  • Now we will write a C program on how to swap two strings.



Program #1 Write a C program to swap two strings


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main(int argc, char *argv[])
  4. {
  5.  int i=0,j=0,k=0;
  6.   char str1[20],str2[20],temp[20];
  7.   puts("Enter first string");
  8.   gets(str1);
  9.   puts("Enter second string");
  10.   gets(str2);
  11.   printf("Before swapping the strings \n");
  12.   puts(str1);
  13.   puts(str2);
  14.   while(str1[i]!='\0'){
  15.              temp[j++]=str1[i++];
  16.   }
  17.   temp[j]='\0';
  18.   i=0,j=0;
  19.   while(str2[i]!='\0'){
  20.               str1[j++]=str2[i++];
  21.   }
  22.   str1[j]='\0';
  23.   i=0,j=0;
  24.   while(temp[i]!='\0'){
  25.               str2[j++]=temp[i++];
  26.   }
  27.   str2[j]='\0';
  28.   printf("After swapping the strings are\n");
  29.   puts(str1);
  30.   puts(str2);
  31.   getch();
  32. }

  
Output:


swap strings in c program

Write a C program to swap two integer arrays

  • C program to interchange the elements of an array.
  • Now we will see how to swap two integer arrays in C programming by using third array.



Program #1: Write a C program to swap two integer arrays

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main(int argc, char *argv[])
  4. {
  5.   
  6.   int a[5],b[5],c[5],i;
  7.   printf("Enter First array 5 elemnets \n");
  8.   for(i=0;i<5;i++)
  9.   scanf("%d",&a[i]);
  10.   printf("\nEnter Second array 5 elements\n");
  11.   for(i=0;i<5;i++)
  12.             scanf("%d",&b[i]);
  13.   printf("Arrays before swapping\n");
  14.   printf("First array \n");
  15.   for(i=0;i<5;i++){
  16.             printf("%d ",a[i]);
  17.   }
  18.   printf("\nSecond array\n");
  19.   for(i=0;i<5;i++){
  20.             printf("%d ",b[i]);
  21.   }
  22.   for(i=0;i<5;i++){
  23.             c[i]=a[i];
  24.             a[i]=b[i];
  25.             b[i]=c[i];
  26.   }
  27.   printf("\nArrays after swapping\n");
  28.   printf("First array elements \n");
  29.   for(i=0;i<5;i++){
  30.             printf("%d ",a[i]);
  31.   }
  32.   printf("\nSecond array elements \n");
  33.   for(i=0;i<5;i++){
  34.             printf("%d ",b[i]);
  35.   }
  36.  return 0;
  37. }

Output:


swap arrays in c


C Program to swap two numbers without using third variable

  • C program to swap two numbers without using third variable.
  • Yes we can swap two variables without using third variable.
  • We have four different ways to swap two numbers without using third variable.
  • Lets see an example C program on how to swap two numbers without using third variable and without using function.



Program #1: Write a simple C program to swap two numbers without using third variable

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6.   int a,b;
  7.   printf("Please enter two integer numbers to swap\n");
  8.   scanf("%d%d",&a,&b);
  9.   printf("before swapping\n");
  10.   printf("a=%d\n",a); 
  11.   printf("b=%d",b);   
  12.   
  13.     a=b+a;
  14.     b=a-b;
  15.     a=a-b;
  16.     
  17.   printf("\nafter swapping\n");
  18.   printf("a=%d\n",a); 
  19.   printf("b=%d",b); 
  20.   getch();
  21.   
  22. }

Output:


swap without third variable c program



Program #2: Write a simple C program to swap two numbers without using third variable


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6.   int a,b;
  7.   printf("Please enter two integer numbers to swap\n");
  8.   scanf("%d%d",&a,&b);
  9.   printf("before swapping\n");
  10.   printf("a=%d\n",a); 
  11.   printf("b=%d",b);   
  12.  
  13.   a=a+b-(b=a); 
  14.     
  15.   printf("\nafter swapping\n");
  16.   printf("a=%d\n",a); 
  17.   printf("b=%d",b); 
  18.   getch();
  19.   
  20. }

Output:

  1. Please enter two integers numbers to swap
  2. 30
  3. 40
  4. before swapping
  5. a=30
  6. b=40
  7. after swapping
  8. a=40
  9. b=30

Program #3: Write a simple C program to swap two numbers without using third variable : call by value

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main(int argc, char *argv[])
  4. {
  5.   
  6.   int a=12, b=13;
  7.     a=a^b;
  8.     b=a^b;
  9.     a=b^a;
  10.    printf("a=%d\n",a); 
  11.  printf("b=%d",b); 
  12.   getch();
  13.   
  14. }

  Output:

  1. a=13
  2. b=12

Program #4: Write a simple C program to swap two numbers without using third variable : call by value

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main(int argc, char *argv[])
  4. {
  5.   
  6.   int a=12, b=13;
  7.      a=b-~a-1;
  8.     b=a+~b+1;
  9.      a=a+~b+1;
  10.    printf("a=%d\n",a); 
  11.    printf("b=%d",b); 
  12.  
  13.  return 0;
  14.   
  15. }

  Output:

  1. a=13
  2. b=12

C program to swap two numbers without using third variable and using functions

  • C program to swap two numbers using functions temporary variable.
  • We can swap two numbers by using temporary variable.
  •  Lets see an example C program to swap two numbers without using any function.
  • C program to swap two numbers using third variable


Program #1: Write a C program to swap two numbers using temporary variable without using any function and by using third variable.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6.   int a,b, temp;
  7.   printf("Please enter two integer number to swap\n");
  8.   scanf("%d%d",&a,&b);
  9.   printf("before swapping\n");
  10.   printf("a=%d\n",a); 
  11.   printf("b=%d",b);   
  12.   temp=a;
  13.   a=b;
  14.   b=temp;
  15.   printf("\nafter swapping\n");
  16.   printf("a=%d\n",a); 
  17.   printf("b=%d",b);        
  18.   
  19.  return 0;
  20.   
  21. }
     

 Output:


swap numbers in c.png


Program #2: Write a C program to swap two numbers using temporary variable with using  function and using third variable.

  • C program to swap two numbers using functions call by value

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. void swap(int a, int b);
  4. int main(int argc, char *argv[])
  5. {
  6.   int a,b;
  7.   printf("Please enter two integer numbers to swap\n");
  8.   scanf("%d%d",&a,&b);
  9.   printf("before swapping\n");
  10.   printf("a=%d\n",a); 
  11.   printf("b=%d",b);   
  12.   
  13.   swap(a,b);   
  14.     
  15.   return
  16.   
  17. }
  18.  
  19. void swap(int a, int b){
  20.     
  21.   int temp=a;
  22.   a=b;
  23.   b=temp;
  24.   printf("\nafter swapping\n");
  25.   printf("a=%d\n",a); 
  26.   printf("b=%d",b); 
  27.     
  28. }

Output:

  1. Please enter two integer numbers to swap
  2. 10
  3. 20
  4. before swapping
  5. a=10
  6. b=20
  7. after swapping
  8. a=20
  9. b=10

C program to print patterns of numbers,alphabets and stars in a pyramid shape

  • We can print *'s  in pyramid pattern using C programming language.
  • We can also print  patterns of numbers and alphabets using C program.
  • Now Check below program that how to print pyramid pattern of * using C program using for loop.
  • C program to print patterns of numbers and stars in a pyramid shape



Program #1 : Write a C program to print pyramid pattern of numbers using for loop.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(){
  5.     
  6.    int i, j, rows;
  7.  
  8.     printf("Enter number of rows: \n");
  9.     scanf("%d",&rows);
  10.  
  11.     for(i=1; i<=rows; ++i)
  12.     {
  13.         for(j=1; j<=i; ++j)
  14.         {
  15.             printf("* ");
  16.         }
  17.         printf("\n");
  18.     }
  19.         
  20.    getch();
  21.     
  22. }
   
Output:


print pyramid pattern c program

 Program #2 : Write a C program to print pyramid pattern of numbers using for loop.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(){
  5.     
  6.    int i, j, rows;
  7.  
  8.     printf("Enter number of rows to print pyramid numbers pattern \n ");
  9.     scanf("%d",&rows);
  10.  
  11.     for(i=1; i<=rows; ++i)
  12.     {
  13.         for(j=1; j<=i; ++j)
  14.         {
  15.             printf("%d ",j);
  16.         }
  17.         printf("\n");
  18.     }
  19.         
  20.    return 0;
  21.     
  22. }

Output:

  1. Enter number of rows to print pyramid numbers pattern
  2.  10
  3. 1
  4. 1 2
  5. 1 2 3
  6. 1 2 3 4
  7. 1 2 3 4 5
  8. 1 2 3 4 5 6
  9. 1 2 3 4 5 6 7
  10. 1 2 3 4 5 6 7 8
  11. 1 2 3 4 5 6 7 8 9
  12. 1 2 3 4 5 6 7 8 9 10
  
Program #3 : Write a c program to print patterns of numbers and alphabets

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(){
  5.     
  6.    int i, j;
  7.     char input, alphabet = 'A';
  8.  
  9.     printf("Enter the uppercase character you want to print in last row: ");
  10.     scanf("%c",&input);
  11.  
  12.     for(i=1; i <= (input-'A'+1); ++i)
  13.     {
  14.         for(j=1;j<=i;++j)
  15.         {
  16.             printf("%c", alphabet);
  17.         }
  18.         ++alphabet;
  19.  
  20.         printf("\n");
  21.     }
  22.     
  23.     getch();
  24.     
  25. }

Output:

  1. Enter the uppercase character you want to print in last row:
  2.  I
  3. A
  4. BB
  5. CCC
  6. DDDD
  7. EEEEE
  8. FFFFFF
  9. GGGGGGG
  10. HHHHHHHH
  11. IIIIIIIII

C program to find leap year using conditional operator

  • We have already seen how to check leap year or not using C program here
  • Now we need to find out a leap year by using conditional operator using C programming language.
  • expression ? value1 : value2
  • if expression is true then it picks value1 else value2.
  • Let us see an example C program to check a year is leap year or not by using conditional operator.



Program #1: Write a C program to find leap year using conditional operator without using any function and loops.


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(){
  5.     
  6.     int year;
  7.     printf("Enter a year to check if it is a leap year or not \n");
  8.     scanf("%d", &year);
  9.  
  10.    
  11.     (year%4==0 && year%100!=0) ? printf("Leap Year") :
  12.         (year%400 ==0 ) ? printf("Leap Year") : printf("not a leap year");
  13.         
  14.    getch();
  15.     
  16. }

Output:


  1. Enter a year to check if it is a leap year or not
  2. 2012
  3. Leap Year


leap year in c conditional operator

this program prompts the user to enter a year and then uses the conditional operator to check if the year is a leap year. The condition being checked is whether the year is evenly divisible by 4 and not divisible by 100, or if it is evenly divisible by 400. If the year is a leap year, the program will print "year is a leap year.", otherwise it will print "year is not a leap year."

The conditional operator is represented by the ? symbol. It takes three operands: a condition, an expression to execute if the condition is true, and an expression to execute if the condition is false.

In this example, the condition is (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0), the first expression to execute if the condition is true is printf("%d is a leap year.\n", year) and the second expression if the condition is false is printf("%d is not a leap year.\n", year)

It's important to note that this program doesn't take into account the Julian calendar and leap year rules before the Gregorian calendar.

In summary, this is a simple C program that uses the conditional operator to check if a year is a leap year, by evaluating a condition that checks if the year is evenly divisible by 4 and not divisible by 100, or if it is evenly divisible by 400. The program uses the ternary operator to make the code more concise and readable.
Select Menu