Input and Output for Strings in C Language

Bhanu Priya
Updated on 08-Mar-2021 10:27:24

5K+ Views

An array of characters (or) collection of characters is called a string.Input and output for stringsExampleFollowing is the C program for input and output for strings −#include main ( ){    char a[30];    printf("enter your name");    scanf ( "%s", a);    printf ("your name is %s", a);    getch ( ); }OutputWhen the above program is executed, it produces the following result −1. Enter your name : Lucky 2. Enter your name : Lucky Lol Your name is Lucky Your name is LuckyNote −‘&’ is not used for accepting strings because name of the string itself specifies the ... Read More

What is a String: Declare and Initialize Strings in C Language

Bhanu Priya
Updated on 08-Mar-2021 10:26:10

7K+ Views

An array of characters (or) collection of characters is called a string.DeclarationRefer to the declaration given below −char stringname [size];For example - char a[50]; a string of length 50 characters.InitializationThe initialization is as follows −Using single character constant −char string[20] = { ‘H’, ‘i’, ‘l’, ‘l’, ‘s’ ,‘\0’}Using string constants −char string[20] = "Hello":;‘\0’ is called a null character. It marks the end of the string.‘\0’ is automatically placed by the compiler, if a string is given as input. The user has to take care of placing ‘\0’ at the end if a single character is given.Accessing − There is ... Read More

C Program to Find Fibonacci Series for a Given Number

Bhanu Priya
Updated on 08-Mar-2021 10:23:36

3K+ Views

Fibonacci Series is a sequence of numbers obtained by adding the two previous numbers.Fibonacci series starts from two numbers f0 & f1.The initial values of fo & f1 can be taken 0, 1 or 1, 1 Fibonacci series satisfies the following conditions −fn = fn-1 + fn-2AlgorithmRefer to the algorithm for the Fibonacci series.START Step 1: Read integer variable a, b, c at run time Step 2: Initialize a=0 and b=0 Step 3: Compute c=a+b Step 4: Print c Step 5: Set a=b, b=c Step 6: Repeat 3 to 5 for n times STOPExampleFollowing is the C program for the ... Read More

Count Vowels and Consonants in a String in C Language

Bhanu Priya
Updated on 08-Mar-2021 10:19:18

3K+ Views

ProblemHow to write a C program to count numbers of vowels and consonants in a given string?SolutionThe logic that we will write to implement the code to find vowels and consonants is −if(str[i] == 'A' || str[i] == 'E' || str[i] == 'I' || str[i] == 'O' || str[i] == 'U'||str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u' )If this condition is satisfied, we try to increment the vowels. Or else, we increment the consonants.ExampleFollowing is the C program to count the number of vowels and consonants in a ... Read More

Structure Declaration in C Language

Bhanu Priya
Updated on 08-Mar-2021 10:15:03

13K+ Views

The structure is a collection of different datatype variables, grouped together under a single name. It is a heterogeneous collection of data items that share a common name.Features of structureIt is possible to copy the contents of all structural elements of different data types to another structure variable of its type by using an assignment operator.To handle complex datatypes, it is possible to create a structure within another structure, which is called nested structures.It is possible to pass an entire structure, individual elements of structure, and address of structure to a function.It is possible to create structure pointers.The general form ... Read More

Find a Leap Year Using C Language

Bhanu Priya
Updated on 08-Mar-2021 10:09:07

379 Views

Leap year is a year that consists of 366 days. For every four years, we will experience a leap year.The logic we will implement to find whether the given year by the user through the console is a leap or not −if (( year%400 == 0)|| (( year%4 == 0 ) &&( year%100 != 0)))If this condition is satisfied, then the given year is a leap year. Otherwise, it is not.ExampleFollowing is the C program to check leap year with the help of If condition − Live Demo#include int main(){    int year;    printf("Enter any year you wish ... Read More

Display Complete Text One Word Per Line in C Language

Bhanu Priya
Updated on 08-Mar-2021 10:06:44

1K+ Views

First, open the file in write mode. Later on, enter the text until it reaches the End Of File (EOF) i.e. press ctrlZ to close the file.Again, open in reading mode. Then, read words from the file and print each word in a separate line and close the file.The logic we implement to print one word per line is as follows −while ((ch=getc(fp))!=EOF){    if(fp){       char word[100];       while(fscanf(fp, "%s", word)!=EOF) // read words from file{          printf("%s", word); // print each word on separate lines.       }     ... Read More

Accessing Structure Variable in C Language

Bhanu Priya
Updated on 08-Mar-2021 10:05:00

1K+ Views

The structure is a user-defined data type, which is used to store a collection of different data types of data.The structure is similar to an array. The only difference is that an array is used to store the same data types whereas, the structure is used to store different data types.The keyword struct is for declaring the structure.Variables inside the structure are the members of the structure.A structure can be declared as follows −Struct structurename{    //member declaration };ExampleFollowing is the C program for accessing a structure variable − Live Demostruct book{    int pages;    float price;    char author[20]; ... Read More

Access Array Element in C Language

Bhanu Priya
Updated on 08-Mar-2021 10:02:11

7K+ Views

An array is a group of related data items that share a common name. A particular value in an array is identified by using its "index number" or "subscript".The advantage of an array is as follows −The ability to use a single name to represent a collection of items and to refer to an item by specifying the item number enables the user to develop concise and efficient programs.The syntax for declaring array is as follows −datatype array_name [size];For example, float height [50]This declares ‘height’ to be an array containing 50 float elements.int group[10]This declares the ‘group’ as an array ... Read More

Find Profit or Loss in Buying an Article using C Program

Bhanu Priya
Updated on 08-Mar-2021 09:55:39

335 Views

The formula for finding profit is as follows if the selling price is greater than cost price −profit=sellingPrice-CosePrice;The formula for finding loss is as follows if cost price is greater than selling price −loss=CostPrice-SellingPriceNow, apply this logic in the program and try to find whether the person gets a profit or loss after buying any article −ExampleFollowing is the C program to find the profit or loss − Live Demo#include int main(){    float CostPrice, SellingPrice, Amount;    printf(" Enter the product Cost : ");    scanf("%f", &CostPrice);    printf(" Enter the Selling Price) : ");    scanf("%f", &SellingPrice);    if ... Read More

Advertisements