0% found this document useful (0 votes)
10 views5 pages

Pps Practical7 Solution Without Watermark-2

The document contains programs to perform string operations like length, uppercase, lowercase, reverse, copy, concatenate, count vowels, check palindrome and sort characters of a string both with and without built-in functions. It also includes a program to take a sentence as input and count total characters and words.

Uploaded by

vibhuim1507
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)
10 views5 pages

Pps Practical7 Solution Without Watermark-2

The document contains programs to perform string operations like length, uppercase, lowercase, reverse, copy, concatenate, count vowels, check palindrome and sort characters of a string both with and without built-in functions. It also includes a program to take a sentence as input and count total characters and words.

Uploaded by

vibhuim1507
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
You are on page 1/ 5

Ganpat university

U.V.Patel college of Engineering


Programming of Problem Solving
Practical:-7
7.1 Write a program to find length of the string.
(a) Without using inbuilt function strlen().
(b) Using strlen()
a.
#include<stdio.h>
int main(){
char kitty[99];
printf("enter any string \n");
gets(kitty);
int len=0;
for(int i=0;kitty[i]!='\0';i++){
len++;
}
printf("The length of the string is %d",len);
return 0;
}
b.
#include<stdio.h>
#include<string.h>
int main(){
char kitty[99];
printf("enter any string \n");
gets(kitty);
printf("the length of the string is %d",strlen(kitty));
return 0;
}
7.2 Write a program to convert string into uppercase.
(a) using strupr()
(b) without using strupr()
a. #include<stdio.h>
int main(){
char kitty[99];
printf("enter any string in lowercase \n");
gets(kitty);
for(int i=0;kitty[i]!='\0';i++){
if(kitty[i]>='a'&&kitty[i]<='z'){
kitty[i]=kitty[i]-32;
}
}
puts(kitty);
return 0;
}
b. #include<stdio.h>
#include<string.h>
int main(){
char kitty[99];
printf("enter the string in lowercase \n");
gets(kitty);
strupr(kitty);
puts(kitty);
return 0;
}
7.3 Write a program to reverse a string.
(a) Without using inbuilt function strrev().
(b) Using strrev()
a. #include<stdio.h>
int main(){
char k[99],h[99];
int b,end,c=0;
printf("enter the string \n");
gets(k);
while(k[c]!='\0'){
c++;
end=c-1;
}
for(b=0;b<c;b++){
h[b]=k[end];
end--;
}
puts(h);
return 0;
}
b. #include<stdio.h>
#include<string.h>
int main(){
char k[99];
printf("enter the string \n");
gets(k);
printf("%s",strrev(k));
}
7.4 Write a program to copy one string to another.
(a) with using inbuilt function strcpy()
(b) without using strcpy()
a. #include<stdio.h>
int main(){
char str1[99];
printf(“enter the string \n”);
gets(str1);
char str2[99];
int i=0;
while(str1[i]!='\0'){
str2[i]=str1[i];
i++;
}
puts(str2);
return 0;
}
b. #include<stdio.h>
#include<string.h>
int main(){
char str1[99];
printf(“enter the string \n”);
gets(str1);
char str2[99];
strcpy(str2,str1);
puts(str2);
return 0;
}
7.5 Write a program to concatenate two strings.
(a) using inbuilt function strcat()
(b) without using strcat()
a. #include<stdio.h>
#include<string.h>
int main(){
char str1[99]="hello";
char str2[99]="world";
int len=strlen(str1);
int i=0;
while(str2[i]!='\0'){
str1[len]=str2[i];
i++;
len++; }
puts(str1);
return 0;
}
b. #include<stdio.h>
#include<string.h>
int main(){ char str1[99]="hello";
char str2[99]="world";
strcat(str2,str1);
puts(str2);
return 0;
}
7.6 Write a program to convert string into lowercase.
(a) using inbuilt function strlwr()
(b) without using strlwr()
a. #include<stdio.h>
int main(){
char kitty[99];
printf("enter any string in uppercase \n");
gets(kitty);
for(int i=0;kitty[i]!='\0';i++){
if(kitty[i]>='A'&&kitty[i]<='Z'){
kitty[i]=kitty[i]+32;
}
}
puts(kitty);
return 0;
}
b. #include<stdio.h>
#include<string.h>
int main(){
char kitty[99];
printf("enter the string in uppercase \n");
gets(kitty);
strlwr(kitty);
puts(kitty);
return 0;
}
7.7 Write a program to Count no. of Vowels in given String.
#include<stdio.h>
int main(){
char k[99];
int c=0;
printf("enter the string \n");
gets(k);
for(int i=0;k[i]!='\0';i++){

if(k[i]=='a'||k[i]=='e'||k[i]=='i'||k[i]=='o'||k[i]=='u'||k[i]=='A'||k[i]=='E'||k[i]=='I'||k[i]=='O'||k[i]=='U'){
c++;
}
}
printf("Number of vowels in this string is %d",c);
return 0;
}
7.8 Write a program to check whether given String is Palindrome or not.
#include<stdio.h>
#include<string.h>
int main(){
char str1[99];
printf("enter the string \n");
gets(str1);
int l=strlen(str1)-1;
int count=0;
for(int i=0;i<=(strlen(str1)/2);i++){
if(str1[i]!=str1[l]){
count++;
}
l--;
}
if(count==0){
printf("the string is palidrome");
}else if(count!=0){
printf("the number is not palidrome");
}
return 0;
}
7.9 Make a program to sort the characters for the given string.
#include<stdio.h>
#include<string.h>
int main(){
char k[99];
printf("enter the string \n");
gets(k);
int l;
l=strlen(k);
for(int i=0;i<l-1;i++){
for(int j=0;j<l-i-1;j++){
if(k[j]>k[j+1]){
int temp;
temp=k[j];
k[j]=k[j+1];
k[j+1]=temp;
}
}
}
puts(k);
return 0;
}
7.10 Take a sentence in a string using scanf without for loop. Now find out total no of
characters and words in that sentence excluding spaces.
#include <stdio.h>
int main() {
char k[100];
int c = 0, w = 1,i;
printf("Enter a String: \n");
scanf("%[^\n]%*c", k);
for (i=0 ; k[i] != '\0' ; i++){
if (k[i] == ' '){
w++;
}else{
c++;
}
}
printf("Total characters: %d\n", c);
printf("Total words: %d\n", w);
return 0; }

You might also like