You are on page 1of 8

Strings

Extra Programs

11/06/2023 CSE 1001 Department of CSE 1


Strings Bubble Sort

for(i=0;i<no-1;i++)
for(j=i+1;j<no;j++)
int main()
{
{ if(strcmp(string[i],string[j])>0)
{
char string[30][30],temp[30];
strcpy(temp,string[i]);
int no, i, j;
cout<<"\nEnter the no of strings:"; strcpy(string[i],string[j]);
strcpy(string[j],temp);
cin>>no; }
cout<<"\nEnter the strings:"; }

for(i=0;i<no; i++) cout<<"\nThe sorted array is:";


gets(string[i]); for(i=0;i<no;i++)
puts(string[i]);
return 0;
}

11/06/2023 CSE 1001 Department of CSE 2


String Bubble Sort input/output

D E L H I \0

A G R A \0

B A R E L I \0

A G R A \0

B A R E L I \0

D E L H I \0

11/06/2023 CSE 1001 Department of CSE 3


Finding Sub-string
len=strlen(str);
#include<stdio.h> Printf("length of main string is %d\n “,len);

#include<string.h> printf(" enter the substring “);

#include<stdlib.h>
gets(substr1);

int main()
for(i=0;str[i]!='\0';i++)
{ char str[50], substr1[50];
{
int i,pos,k,len,j;
j=0;
cout<<"enter the main string "<<endl;
if(str[i]==substr1[j])
gets(str); {
pos=i;
j++;

11/06/2023 CSE 1001 Department of CSE 4


Finding Sub-string
for(k=pos+1;j<strlen(substr1);k+
+,j++)
{
if(str[k]==substr1[j])
continue;
else
break;
}
}
if(j==strlen(substr1)){
c++;
if(i!=strlen(str)
continue;}
}

11/06/2023 CSE 1001 Department of CSE 5


o/p

11/06/2023 CSE 1001 Department of CSE 6


Deleting Repeating words from afor(i=0;i<=n;i++)
sentence
{
for(j=i+1;j<=n;j++)
int main()
{
{
char sent[50],temp[50], str[10][50]; if(strcmp(str[i],str[j])==0)
int i=0,j=0,n=0,k,r=0; {
Printf("enter a sentence\n“); int pos=j;
fflush(stdin); if(pos==n)
gets(sent);
puts(sent);
n=n-1;
for(i=0;;i++) else{
{ for(k=pos;k<n;k++)
if(sent[i]!=' '){ strcpy(str[k],str[k+1]);
str[n][j+ n=n-1;} }}}
+]=sent[i];
}
printf("after deleting“);
else{ for(i=0;i<=n;i++)
str[n][j++]='\0'; {
n++; for(j=0;str[i][j]!='\0';j++)
j=0; {
}
if(sent[i]=='\0')
temp[r++]=str[i][j]; }
break; } temp[r++]=' '; }
temp[r++]='\0';
11/06/2023 CSE 1001 puts(temp);}
Department of CSE 7
o/p

11/06/2023 CSE 1001 Department of CSE 8

You might also like