0% found this document useful (0 votes)
1 views11 pages

String Final

Uploaded by

sachinphogat9050
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views11 pages

String Final

Uploaded by

sachinphogat9050
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

NotesBundle

PROGRAMMING IN C
LAB
String
BY : -NotesBundle
Strings
1. Write a program to read a line of text and print it by using

i)scanf( ) and printf( ) functions

ii) gets( ) and puts( ) functions

iii) getchar( ) and putchar( ) functions

2. Write a program to read 80-character string and find total no. of vowels (using switch

case).Write a program to sort a list of names in alphabetical order

3. Write a program to read a line of text and count the no. of words and characters.
Program 1-A
// Input a string using scanf()

#include<stdio.h>

#include<conio.h>

void main()

char string[50];

clrscr();

printf(" Entering a string using scanf()\n\n");

printf(" Enter a line : ");

scanf("%[^\n]s",string);

printf("\n\n The line is : %s",string);

getch();

}
Program 1-B
// Input a string using gets()

#include<stdio.h>

#include<conio.h>

void main()

char string[50];

clrscr();

printf(" Input sting using gets()\n\n");

printf(" Enter a string :\n\n");

gets(string);

printf("\n\n The string is :\n\n");

puts(string);

getch();

}
Program 1-C
// Input a string using getchar()

#include<conio.h>

#include<stdio.h>

void main()

int i,len;

char a[50];

clrscr();

printf(" Input a string using getchar() \n\n");

printf(" Enter a line : (Press '0' to end the string)\n\n ");

for(i=0;a[i-1]!='0';i++)

a[i]=getchar();

len=i;

printf("\n\n The line is : \n\n ");

for(i=0;i<len-1;i++)

putchar(a[i]);

getch();

}
Program 2-A
// To count number of vowels in a line

#include<stdio.h>

#include<conio.h>

void main()

int i,ctr=0;

char a[80];

clrscr();

printf(" Count vowels in a line \n\n");

printf(" Enter a line : ");

gets(a);

for(i=0;a[i]!='\0';i++)

switch(a[i])

case 'a':

case 'e':

case 'i':

case 'o':

case 'u': ctr++;

break;

}
}

printf("\n\n The number of vowels is %d",ctr);

getch();

}
Program 2-B
// To sort a list of names in ascending order

#include<stdio.h>

#include<conio.h>

#include<string.h>

#include<ctype.h>

void main()

int i,j,k=0;

/*/////////////////////////////////////////////////////////////

->Here i and j are basic loop controllers.

->k helps to traverse inside the string to clarify between adjacent

letters in case of the earlier letters being same.

eg. aakash and aamir (sort b/w k and m)

anand and adnan (sort b/w n and d)

/////////////////////////////////////////////////////////////*/

char a[10][50],temp[50];

clrscr();

// Accepting names
printf(" Ascending order of names \n\n");

printf(" Enter 5 names : ( IN SECOND CASE LETTERS )\n\n");

for(i=0;i<5;i++)

printf("%d. ",i+1);

scanf(" %[a-z]",&a[i]);

// Sorting in ascending order using bubble sort technique

for(i=0;i<4;i++)

for(j=0;j<4;j++)

if(a[j+1][k]==a[j][k])

k++;

if(a[j+1][k]<a[j][k])

strcpy(temp,a[j]);

strcpy(a[j],a[j+1]);

strcpy(a[j+1],temp);

}
k=0;

printf("\n\nThe list in alphabetical order of first name\n\n");

for(i=0;i<5;i++)

printf("\n%d. %s",i+1,a[i]);

getch();

}
Program 3
#include<conio.h>

#include<stdio.h>

#include<ctype.h>

void main()

char line[80];

int char_ctr=0,word_ctr=0,i;

clrscr();

printf(" Calcultion of no. of characters and words ");

printf("\n\n Enter a line : ");

gets(line);

for(i=0;line[i]!='\0';i++)

if(line[i]==' ') word_ctr++;

if((line[i]>='a'&&line[i]<='z')||(line[i]>='A'&&line[i]<='Z')) char_ctr++;

word_ctr++;

printf(" \n\n The number of words is %d",word_ctr);

printf(" \n The number of characters is %d",char_ctr);

getch();

You might also like