Skip to main content

Posts

Showing posts with the label C Interview Question and Answers

Troubleshooting Guide: Windows 11 Taskbar Not Showing - How to Fix It

  If your Windows 11 taskbar is not showing, you can try several troubleshooting steps to resolve the issue. Here are some potential solutions you can try:

Count Number of words in a String in C Language?

In C language, you can count the number of words in a string using the following steps: Take the input string from the user. Initialize a variable word_count to 1. This is because the first word in the string is always preceded by a space. Traverse the string character by character using a loop. If a space or a newline character is encountered, increment the value of word_count . Continue traversing the string until the end is reached. Print the final value of word_count . Here is an example program that counts the number of words in a string: c # include <stdio.h> int main () { char str[ 100 ]; int word_count = 1 , i; printf ( "Enter a string: " ); fgets(str, 100 , stdin ); for (i = 0 ; str[i] != '\0' ; i++) { if (str[i] == ' ' || str[i] == '\n' ) { word_count++; } } printf ( "Number of words: %d" , word_count); return 0 ; } In this program, the input string is

How to Reverse a String in C Language?

  In C language, you can reverse a string using the following steps: Take the input string from the user. Find the length of the string using the strlen() function. Swap the first character with the last character, the second character with the second-last character, and so on until the middle of the string is reached. Print the reversed string. Here is an example program that reverses a string: #include <stdio.h> #include <string.h> int main() { char str [ 100 ], temp; int i, j, len ; printf( "Enter a string: " ); scanf( "%s" , str ); len = strlen( str ); for (i = 0 , j = len - 1 ; i < j; i++, j--) { temp = str [i]; str [i] = str [j]; str [j] = temp; } printf( "Reversed string: %s" , str ); return 0 ; } In this program, the input string is taken from the user using the scanf() function. The length of the string is calculated using the strlen() function. The for lo

How to copy data from one file to another file in C Language?

  In C language, you can copy data from one file to another using the following steps: Open the source file in read mode and the destination file in write mode using the fopen() function. Check if both files were opened successfully. Read data from the source file using the fread() function and write it to the destination file using the fwrite() function. Repeat step 3 until the end of the source file is reached. Close both files using the fclose() function. Here is an example program that copies data from one file to another: c # include <stdio.h> int main () { FILE *source_file, *dest_file; char ch; source_file = fopen( "source.txt" , "r" ); if (source_file == NULL ) { printf ( "Error opening source file." ); return 1 ; } dest_file = fopen( "destination.txt" , "w" ); if (dest_file == NULL ) { printf ( "Error opening destination file." ); retur

Binary to decimal conversion in C Language

  In C language, you can convert a binary number to decimal using the following steps: Read the binary number as a string or an integer. Initialize a decimal variable to 0. Set a variable 'base' to 1, which will be used to calculate the decimal equivalent of each binary digit. Starting from the rightmost digit, extract each digit of the binary number and multiply it with the base. Add the result of the multiplication to the decimal variable. Multiply the base by 2 and repeat steps 4 to 6 for each digit of the binary number, moving from right to left. The final value of the decimal variable will be the decimal equivalent of the binary number. Here is an example program that converts a binary number to decimal: csharp #include <stdio.h> int main () { int binary, decimal = 0 , base = 1 , rem; printf( "Enter a binary number: " ); scanf( "%d" , &binary); while (binary > 0 ) { rem = binary % 10 ; decim