Modue 2
Modue 2
CHAPTER 11 Strings
The function gets() and puts( ) are called line oriented I/O functions
and can be used to process entire line. The gets() function can be
used to read entire line including white spaces. The puts() function
can be used to print the line.
Reading a string using gets(): The general for of gets function is
as given below
gets(str);
Printing a string using puts(): The general form of puts function
is as given below
puts(str);
PROGRAM :
#include <stdio.h>
void main()
{
char name[7];
printf("Enter your name \n");
gets(name);
printf("Your name is \n");
puts(name);
}
Output:
1)Enter your name: Sahil
Your name is: Sahil
2)Enter your name: Prasad
Your name is: Prasad
void main()
{
char lwr[30];
printf("Enter a string of lower case characters : ");
scanf("%s",lwr);
strupr(lwr);
printf("The Entered string in upper case character is :%s",lwr);
}
Output:
1.Enter a string of lower case characters : prashanth
The Entered string in upper case character is :PRASHANTH
2.Enter a string of lower case characters : sai
The Entered string in upper case character is :SAI
6.strlwr():
This function coverts uppercase characters to lowercase.
Syntax:
strlwr(str);
PROGRAM:
#include <stdio.h>
#include <string.h>
void main()
{
char upr[30];
printf("Enter a string of upper case characters : ");
scanf("%s",upr);
strlwr(upr);
printf("The Entered string in lower case character is :%s",upr);
}
Output:
1.Enter a string of upper case characters : RCB
The Entered string in lower case character is :rcb
2.Enter a string of upper case characters : CSK
The Entered string in lower case character is :csk
EXAMPLE:
#include <stdio.h>
int main() {
char name[20];
int age;
float salary;
return 0;
}
OUTPUT:
Name: John
Age: 25
Salary: 123.45
sprintf( )
sprintf() is a function in the C programming language used to
write formatted data to a string.
It stands for "String Print Formatted".
It is part of the C Standard Library and is declared in the
<stdio.h> header file.
Syntax:
The function prototype of sprintf() is:
str is the character array (string) where the formatted data will
be stored.
EXAMPLE:
#include <stdio.h>
int main() {
char str[100];
int age = 25;
float salary = 123.45;
sprintf(str, "My age is %d and my salary is %.2f", age, salary);
printf("%s\n", str);
return 0;
}
OUTPUT:
My age is 25 and my salary is 123.45
#include <stdio.h>
#include <string.h>
int main()
{
char name[21];
int i,len;
printf("Enter your name : \n");
gets(name);
len = strlen(name);
printf("The name is ASCII form : \n");
for(i=0;i<len;i++)
{
printf("%d\t",name[i]);
}
return 0;
}
OUTPUT:
1.Enter your name :
Veeru
The name is ASCII form :
86 101 101 114 117
2. Enter your name :
Abi
The name is ASCII form :
65 98 105
#include <stdio.h>
void main() {
char a[21];
int length = 0;
printf("Enter a string: ");
gets(a);
for (int i = 0; a[i] != '\0'; i++) {
length++;
}
printf("The length of the string is: %d\n", length);
}
Output :
1.Enter a string : Helloworld
The length of the string is: 10
#include <stdio.h>
int main() {
char str[100];
int alphabets = 0, digits = 0, i = 0;
Outputs:
1) Enter a string: 4 6 8
Total number of alphabets: 0
Total number of digits: 3
2) Enter a string: T 8 H K 9 R 0 0
Total number of alphabets: 4
Total number of digits: 4
#include<stdio.h>
#include<string.h>
int main (void)
{
char string = ”welcometocprogramming”;
char temp;
int i,j;
int n = strlen(string);
printf(“String before sorting - %s \n”,string);
for (i = 0 ; i < n-1 ; i++)
{
for (j = i+1 ; j<n ; j++)
{
if (string[i] > string[j])
{
temp = string[i];
string[i] = string[j];
string[j] = temp;
}
}
}
printf(“String after sorting - %s \n”,string);
return 0;
}
OUTPUT:
String before sorting – welcometocprogramming
String after sorting – acceeggilmmmnoooprrtw
#include <stdio.h>
int main()
{
char s1[] = "TajMahal"; // String Given
char s2[8]; // Variable to store reverse string
int length = 0;
int loop = 0;
while(s1[length] != ‘\0’)
{
length++;
}
printf("\n Printing in reverse - ");
for(loop = --length; loop>=0; loop--)
printf("%c", s1[loop]);
return 0;
}
OUTPUT:
1. Printing in reverse - lahaMjaT
#include <stdio.h>
int main() {
char a[20];
printf("Enter the string :");
gets(a);
char b[20];
stringCopy(a, b);
Output:
1.
Enter the string :Hello
Copied string: Hello
2.
Enter the string :World
Copied string: World
#include<stdio.h>
Int main()
{
Char s1[ ]=”advise”;
Char s2[ ]=”advice”;
Int n=0;
Int flag=1;
while(s1[n]!=’\o’)
{
If(s1[n]!=s2[n])
{
flag=0;
break;
}
n++;
}
If(flag==1)
{
Printf(“%s and %s are identical\n”,s1,s2);
}
else
{
Printf(“%s and %s are NOT identical\n”,s1,s2);
}
return 0;
}
OUTPUT:
advise and advice are NOT identical
int i=0,alphabets=0,digits=0;
char str[100];
gets(str);
while(str[i] != '\0')
if(isalpha(str[i]) != 0) alphabets++;
i++;
Number of digits = 4
Q18. Write a c program to write vowels and consonants in a
string.
#include<stdio.h>
#include<string.h>
Void main()
{
Char a[21];
Int I,len,vow=0,cons=0;
Printf(“enter the string\n”);
Gets(a);
Strupr(a);
Len=strlen(a);
For(i=0;i<len;i++)
{
If(isalpha(a[i]))
{
If(a[i]==’A’||a[i]==’E’||a[i]==’I’||a[i]==’O’||a[i]==’U’)vow++;
Else cons++;
}
}
Printf(“no. of vowels = %d\n”,vow);
Printf(“no. of consonants = %d\n”,cons);
}
Output:
1.Enter the string: Hello world
No of vowels: 3
No of consonants: 7
Q19. Write a C program that uses a function to reverse the
elements of an array in place. The function must accept
only one pointer value and return void.
Logic: The revArray function accepts a pointer to the array and its size
as arguments. It iterates through the first half of the array and swaps
the elements with their corresponding elements from the second
half, effectively reversing the array in place.
In the main function, the user inputs the size of the array and its
elements. The original array is printed. The revArray function is called
to reverse the array. The reversed array is printed.
Program:
#include <stdio.h>
void revArray(int *array, int size) {
int temp;
for (int i = 0; i < size / 2; i++) {
temp = array[i];
array[i] = array[size - i - 1];
array[size - i - 1] = temp;
}
}
int main() {
int size, array[100], i;
printf("c ");
scanf("%d", &size);
printf("Enter %d elements for the array:\n", size);
for (i = 0; i < size; i++) {
scanf("%d", &array[i]);
}
printf("Original array: ");
for (i = 0; i < size; i++) {
printf("%d ", array[i]);
}
printf("\n");
revArray(array, size);
printf("Reversed array: ");
for (i = 0; i < size; i++) {
printf("%d ", array[i]);
}
printf("\n");
return 0;
}
Output: 1. Enter the size of the array: 3
Enter 3 elements for the array: 2 5 7
Original array: 2 5 7
Reversed array: 7 5 2
2. Enter the size of the array: 5
Enter 3 elements for the array: 2 4 6 8
Original array: 2 4 6 8
Reversed array: 8 6 4 2
Q20. Write a C program using functions to read values
from keyboard into a two-dimensional array, create two
one-dimensional arrays that contain row and column
averages.
Logic: The data structure for calculating row average and column
average is as shown:
The program begins by requesting the user to provide data for a two
dimensional array. For a twodimensional array, a function reading
values from keyboard usually requires nested for loops. If the array is
an n by m array, the first loop varies the row from zero to n — 1. The
second loop varies the column from zero to m – 1.
Once the array has been filled, the program calculates the average of
each row and places it in a parallel array of row averages. It then
calculates the average for each column and places it in an array of
column averages. Although we have represented the column average
array horizontally and the row average array vertically, they are both
one-dimensional arrays.
When all the calculations are complete, the program calls a function
to print the array with the row averages at the end of each row and
the column averages at the bottom of each column.
Program:
#include <stdio.h>
#define MAX_ROWS 3
#define MAX_COLS 4
void getData(int table[][MAX_COLS]);
void rowAverage(int table[][MAX_COLS],float rowAvrg[]);
void colAverage(int table[][MAX_COLS], float colAvrg[]);
void printTables(int table[][MAX_COLS],float rowAvrg[],
float colAvrg[]);
int main (void){
int table[MAX_ROWS][MAX_COLS];
float rowAve[MAX_ROWS] = {0};
float colAve[MAX_COLS] = {0};
getData(table);
rowAverage(table, rowAve);
colAverage(table, colAve);
printf( "\n" );
printTables(table, rowAve, colAve);
return 0;
}
void getData(int table[][MAX_COLS]){
int row, col;
for (row = 0; row < MAX_ROWS; row++)
for (col = 0; col < MAX_COLS; col++){
printf("\nEnter integer and press Enter key: ");
scanf("%d",&table[row][col]);
}
return;
}
void rowAverage ( int table[][MAX_COLS],float rowAvrg []){
for ( int row = 0; row < MAX_ROWS; row++){
for ( int col = 0; col < MAX_COLS; col++)
rowAvrg[row] += table [row][col];
rowAvrg[row] /= MAX_COLS;
}
return;
}
void colAverage ( int table[][MAX_COLS],float colAvrg []){
for ( int col = 0; col < MAX_COLS; col++) {
for ( int row = 0; row < MAX_ROWS; row++)
colAvrg[col] += table [row][col];
colAvrg[col] /= MAX_ROWS;
}
return;
}
void printTables (int table[ ][MAX_COLS], float rowAvrg[],
float colAvrg[]){
for (int row = 0; row<MAX_ROWS; row++)
{
for (int col=0; col< MAX_COLS; col++)
printf("%6d", table[row][col]);
printf("%6.2f\n", rowAvrg[row]);
}
printf("_________________________________\n");