Shiv Nadar University Chennai
School of Engineering
Department of Computer Science and Engineering (AI & DS)
CS1801 – Programming in C Lab
Name: A.KANCHANA
Date:24/11/2023
Roll No: 23110548
A5: Using Strings in C
Aim:Solve the following problems by using strings in C
Question 1:
1. Read multiple sentences that are separated by “.”. Write a program to find the
sentence with the maximum number of words. Apply the following constraints. If
more than one sentence is with the maximum number of words, display the first
sentence with the maximum words.
Example:
Input: C is a general-purpose computer programming language. It was created in the
1970s by Den nis Ritchie. It remains very widely used and influential.
Output: Sentence with maximum number of words: It was created in the 1970s by
Dennis Ritchie. Number of words: 9
Code:
#include <stdio.h>
# include <string.h>
# include<ctype.h>
int main(void) {
int i,j=0,k=0,c[100],count=0,word[100],word_c=1,n=0;
char a[1000],b[100][1000];
gets(a);
for (i=0;i<strlen(a);i++)
{
if(a[i]!='.')
{
b[j][k]=a[i];k++;count++;
if(a[i]==' ')
{
Shiv Nadar University Chennai
School of Engineering
Department of Computer Science and Engineering (AI & DS)
CS1801 – Programming in C Lab
word_c++;
}
}
else
{
word[j]=word_c;
c[j]=count;
j++;k=0;count=0;word_c=0;
}
}
i=0;k=1;
while(i<j)
{
if(word[i]>word[k]&&k!=j )
{
i=0;k++;
}
else if(k==j)
{
i=j;
}
else
{
i++;k=i+1;n++;
}
}
puts(b[n]);
printf("Total words=%d",word[n]);
return 0;
}
Test Cases: (Minimum 5 Test Cases)
1. my name is sri.i have an elder sister whose name is seetha.i am belongs to a
single parent family
i have an elder sister whose name is seetha
Total words=8
Shiv Nadar University Chennai
School of Engineering
Department of Computer Science and Engineering (AI & DS)
CS1801 – Programming in C Lab
2. BTS is a kpop music band.There are seven boys in the kpop band.It is one of the
most famous kpop band in the world.
It is one of the most famous kpop band in the world
Total words=11
3. i have banana and apples. i have six banana. i ate all of the apples that i have.
i ate all of the apples that i have
Total words=9
4.i completed my schooling in kr saratha govt hr secondary school. i love my
school. my school days are unforgottable.
i completed my schooling in kr saratha govt hr secondary school
Total words=11
5. my hometown is famous for several amazing natural feature
Total words=9
Output: (Terminal Screen Shot)
Shiv Nadar University Chennai
School of Engineering
Department of Computer Science and Engineering (AI & DS)
CS1801 – Programming in C Lab
Question 2:
Write a program which replaces all the occurrences of a substring with another in a given
line of text.
Example: Input: “There are 30 bananas on a banana-tree”, “banana”, “apple”
Output: “There are 30 apples on a apple-tree”.
Code:
# include <stdio.h>
# include <string.h>
# include <ctype.h>
int main(void)
{
char a[1000],b[100],c[100],word[100][100];
int i,j=0,k=0;
printf("enter the sentence\n");
gets(a);
printf( "what word did you want to change:");
scanf("%s",&b);
printf("enter a word for this:");
scanf("%s",&c);
for(i=0;i<strlen(a);i++)
{
if(a[i]!=' ' )
{
if(a[i]=='.')
{
k=k+1;j=0;
word[k][j]=a[i];
k++;j=0;
}
else
{
word[k][j]=a[i];j++;
}
if(strcmp(word[k],b)==0)
{
strcpy(word[k],c);
k++;j=0;
}
}
else
{
Shiv Nadar University Chennai
School of Engineering
Department of Computer Science and Engineering (AI & DS)
CS1801 – Programming in C Lab
k=k+1;j=0;
word[k][j]=a[i];
k++;j=0;
}
}
k++;
for(i=0;i<k;i++)
{
printf("%s",word[i]);
}
return 0;
}
Output: (Terminal Screen Shot)
Shiv Nadar University Chennai
School of Engineering
Department of Computer Science and Engineering (AI & DS)
CS1801 – Programming in C Lab
Question 3:
Write a program to read a given name and then display it in an abbreviated form
Input: Janak Raj Thareja
Output: JRT
Code:
# include <stdio.h>
# include <string.h>
# include <ctype.h>
int main(void)
{
char x[100];
int i;
printf("enter the sendence: ");
gets(x);
for(i=0;i<strlen(a);i++)
{
if(i==0)
{
printf("%c",x[i]);
}
else if (x[i]==' ')
{
i++;
printf("%c",x[i]);
}
}
return 0;
}
Test Cases:
1. enter the sendence: Sri Nithi
SN
2. enter the sendence: Yuva Lakshmi
YL
3. enter the sendence: Jeon Jungkook
JJ
4. enter the sendence: Tae Hyung V
THV
5. enter the sendence: Kim Soek Jin
Shiv Nadar University Chennai
School of Engineering
Department of Computer Science and Engineering (AI & DS)
CS1801 – Programming in C Lab
KSJ
Terminal Screenshot:
Question: 4
Given a list of strings, write a program to rearrange them in alphabetical order.
Example:
Input: Pacific, Atlantic, Caribbean
Output: Atlantic
Caribbean
Pacific
Code:
# include <stdio.h>
# Include <string.h>
# include <ctype.h>
int main(void)
{
char a[100],b[100][1000],tem[100][100];
int i,j=0,k=0;
printf("enter the words:");
gets(a);
for(i=0;i<strlen(a);i++)
{
Shiv Nadar University Chennai
School of Engineering
Department of Computer Science and Engineering (AI & DS)
CS1801 – Programming in C Lab
if(a[i]!=',')
{
b[j][k]=a[i];k++;
}
else
{
j++;k=0;
}
}
j++;
for(i=0;i<j;i++)
{
for(k=0;k<j;k++)
{
if(strcmp(b[i],b[k])<0)
{
strcpy(tem[i],b[i]);
strcpy(b[i],b[k]);
strcpy(b[k],tem[i]);
}
}
for(i=0;i<j;i++)
{
printf("%s,",b[i]);
}
return 0;
}
Test Cases:
1. enter a words:korea, america, india, bangladesh
america, bangladesh, india,korea,
2. enter a words:tree, leaf, bottle, spoon
bottle, leaf, spoon,tree,
3. enter a words:cucumber, egg, apple, cherry
apple, cherry, egg,cucumber,
4. enter a words:plant, vegitable, aeroplane, milk, elephant
aeroplane, elephant, milk, vegitable,plant,
Shiv Nadar University Chennai
School of Engineering
Department of Computer Science and Engineering (AI & DS)
CS1801 – Programming in C Lab
5. enter a words:cock, pepsi, slice, smoodh,frooti
pepsi, slice, smoodh,cock,frooti,
Outcomes:
Learning Outcomes:
To be proficient in handling strings in C
a) string operations