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

Practical 5

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

Practical 5

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

Practical 6 – Define Strings

Name:- Mayuresh Pandey Date: 14th OCT 2024 Roll no:


1. Write a c program to declare and initialize 2 string variables with string values
and print them on the screen
Input:-
#include<stdio.h>
void main()
{
char mystring[]={'k','c',' ','!','!','c','o','l','l','e','g','e','\0'};
char mystring1[]="kc !!!! college";
int i;
printf("%c\n",mystring[1]);
printf("%c\n",mystring[6]);
for(i=0;i<=8;i++)
printf("%c",mystring[i]);
printf("\nOutput 1:%s\n\n", mystring);
printf("\nOutput 2:%s\n\n", mystring1);
}
Output:-
c

kc !!coll

Output 1:kc !!college

Output 2:kc !!!! college


2. Write a c program to find the length of a string

Input:-
#include<stdio.h>

#include<string.h>

void main()

char str1[30]="KC COLLEGE";

printf ("The length of %s string is %d\n",str1,strlen(str1));

Output:-
The length of KC COLLEGE string is 10

3. Write a c program to copy 1 string into another string


Input:-
#include<stdio.h>

#include<string.h>

void main()

char str1[20]="KC COLLEGE";

char str2[30];

strcpy(str2,str1);

printf("Value of string s2 is %s",str2);

Output:-
Value of string s2 is KC COLLEGE
4. Write a c program to declare and initialize 2 string variables with string values
and join both strings
Input:-
#include<stdio.h>

#include<string.h>

void main()

char str1[20]="KC COLLEGE";

char str2[30]="GOOD MORNING";

printf("Value of string s2 is %s",strcat(str2,str1));

}
Output:-
Value of string s2 is GOOD MORNINGKC COLLEGE

5. Write a c program to declare and initialize 2 string variables with string values
and compare both strings
Input:-
#include<stdio.h>

#include<string.h>

void main()

char str1[10]="KC COLLEGE";

char str2[20]="kc college";

int result;

result=strcmp(str1,str2);

printf("The result is %d",result);

Output:-
The result is -1
6. Write a c program to read and display a character using getchar() and putchar()
functions
Input:-
#include <stdio.h>

#include <string.h>

void main()

int c;

printf("Enter the value: \t");

c=getchar();

printf("\n Entered Values are: \t");

putchar(c);

Output:-
Enter the value: 54

Entered Values are: 5

7. Write a c program to read and display a string using gets() and puts()functions
Input:-
#include<stdio.h>
#include<string.h>
void main()
{
char str[50];
printf("Enter the value:");
gets(str);
printf("Entered value is:");
puts(str);
}
Output:-
Enter the value:14

Entered value is:14

8. Write a c program to read a character using getche() function

Input:-
#include <stdio.h>

#include <string.h>

void main()

char flag;

printf("Do you want to continue Y or N :\n");

flag=getche();

if(flag=='Y')

printf("You have entered Yes");

else

printf("You have entered No");

Output:-

Do you want to continue Y or N :

You have entered No


9. Write a c program to find the reverse of a string
Input:-
#include <stdio.h>
#include <string.h>
void main()
{
char name[40]="Hello";
printf("String before strrev():%s\n",name);
printf("String after strrev():%s",strrev(name));

}
Output:-
String before strrev():Hello
String after strrev():olleH

10. Write a c program to convert from uppercase to lowercase of a string

Input:-
#include<stdio.h>

#include<string.h>

void main()

char string[100];

printf("Input a string to convert to lower case\n");

gets(string);

printf("The string in lower case:%s\n",strlwr(string));

Output:-
Input a string to convert to lower case

HELLO WORLD

The string in lower case:hello world


11. Write a c program to convert from lowercase to uppercase of a string
Input:-
#include<stdio.h>

#include<string.h>

void main()

char string[100];

printf("Input a string to convert to upper case\n");

gets(string);

printf("The string in upper case:%s\n",strupr(string));

Output:-
Input a string to convert to upper case

hello world

The string in upper case:HELLO WORLD


12. Write a c program to find the frequency of characters in a string
Input:-
#include<stdio.h>

#include<string.h>

void main()

char str[100],ch;

int i, frequency=0;

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

gets(str);

printf("Enter a character to find its frequency:\n");

scanf("%c",&ch);

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

if(ch==str[i])

++frequency;

printf("Frequency of%c=%d",ch,frequency);

Output:-
Enter a string:

This is the story of a boy who don't like to study

Enter a character to find its frequency:

Frequency ofs=4
13. Write a c program to display the ASCII value of character entered by the user

Input:-

#include<stdio.h>

#include<string.h>

void main()

char ch;

printf("Enter the character:\n ");

scanf("%c",&ch);

printf("ASCII value of character %c is:%d",ch,ch);

Output:-
Enter the character:

ASCII value of character A is:65


14. Write a c program to count vowels, components, digits, and spaces

Inputs:-
#include<stdio.h>

#include<string.h>

void main()

char line[150];

int i, vowels, consonants, digits, spaces;

vowels = consonants = digits = spaces = 0;

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

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

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

if(line[i]=='a'|| line[i]=='e'|| line[i]=='i'|| line[i]=='o'|| line[i]=='u'|| line[i]=='A'||

line[i]=='E'|| line[i]=='I'|| line[i]=='O'|| line[i]=='U')

++vowels;

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

++consonants;

else if(line[i]>='0'&& line[i]<='9')

++digits;

else if (line[i]==' ')

++spaces;

}
printf("Vowels:%d",vowels);

printf("\nConsonants:%d",consonants);

printf("\nDigits:%d",digits);

printf("\nWhite spaces:%d",spaces);

Output:-

Enter a line of string:

there was a person whose name was Gajodhar he was 45 year old

Vowels:19

Consonants:28

Digits:2

White spaces:13

You might also like