0% found this document useful (0 votes)
36 views12 pages

0103EX171049 (Assingment 2)

The document contains 10 code snippets in C programming language. The code snippets demonstrate various string handling functions in C like calculating length of a string, reversing a string, concatenating strings, comparing strings, substring checking, removing duplicates from a string, sorting strings alphabetically.

Uploaded by

Aegont
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)
36 views12 pages

0103EX171049 (Assingment 2)

The document contains 10 code snippets in C programming language. The code snippets demonstrate various string handling functions in C like calculating length of a string, reversing a string, concatenating strings, comparing strings, substring checking, removing duplicates from a string, sorting strings alphabetically.

Uploaded by

Aegont
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/ 12

1.

main()
{
int n,i,sumeven=0,sumodd=0;
printf("Enter numbers of elements\n");
scanf("%d",&n);
int a[n];
printf("Enter elements\n");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
if(a[i]%2==0)
sumeven=sumeven+a[i];
else
sumodd=sumodd+a[i];
}
printf("Sum of even numbers is %d\nSum of odd numbers is
%d\n",sumeven,sumodd);
}

2. main()

int n,i,j,t,k;

printf("Enter numbers of elements\n");

scanf("%d",&n);
int a[n];

printf("Enter elements\n");

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

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

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

for(k=j+1;k<=i;k++)

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

t=a[j];

a[j]=a[k];

a[k]=t;

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

printf("%d ",a[i]);

3.

main()
{
int a[3][3],b[3][3],c[3][3],i,j,n,sum=0;
printf("Enter first matrix elements\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("Enter second matrix elements\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
for(n=0;n<3;n++){
sum=sum+a[i][n]*b[n][j];}
c[i][j]=sum;
sum=0;
}
}
printf("Multiplication of matrices is:-\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%d ",c[i][j]);
printf("\n");
}
}
4. I) main()
{
char a[30];
int i,c=0;
printf("Enter a string\n");
gets(a);
for(i=0;a[i];i++)
if(a[i]==' ')
c++;
printf("Words in your string is %d",c+1);
}

main()

char a[30],i;

printf("Enter a string\n");

gets(a);
i=strlen(a);

printf("Length of string is %d",i);

Ii) main()

char a[30],i;

printf("Enter a string\n");

gets(a);

printf("Reverse string is %s",strrev(a));

Iii) main()

char a[30],i,b[30];

printf("Enter first string\n");

gets(a);

printf("Enter second string\n");

gets(b);

printf("Concatinated string is %s",strcat(a,b));

}
Iv) main()

{
char a[30],b[30];
int i,j,l,c=1,la,k;
printf("Enter a string\n");
gets(a);
printf("Enter string\n");
gets(b);
l=strlen(b);
la=strlen(a);
for(k=0;a[k]!=' ';k++);
for(j=0;j<k;j++)
if(a[j]!=b[j])
break;
if(j==k)
printf("Second string is a substring of first\n");
for(i=k+1;a[i];i++)
{
if(a[i]==' ')
{
if(a[i+1]==b[0])
{
for(j=1;b[j];j++)
if(a[i+1+j]==b[j])
c++;
if(c==l){
printf("Second string is a substring of first\n");
break;
}
}
}
}
if(i==la)
printf("Second string is not a substring of first string\n");
}

V) int main()

char str[100];

int i, j, k;

printf("\n Please Enter any String : ");

gets(str);

for(i = 0; i < strlen(str); i++)

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

if(str[j] == str[i])

{
for(k = j; str[k] != '\0'; k++)

str[k] = str[k + 1];

printf("\n The Final String after Removing All Duplicates = %s ", str);

return 0;

5. main()

{
char a[10][30],b[30];
int i,j;
printf("Enter 10 names\n");
for(i=0;i<10;i++)
gets(a[i]);
for(i=0;i<10;i++)
for(j=i+1;j<10;j++)
if(stricmp(a[i],a[j])==1)
{
strcpy(b,a[i]);
strcpy(a[i],a[j]);
strcpy(a[j],b);
}
printf("\n\n");
for(i=0;i<10;i++)
{
puts(a[i]);
}
}

6. main()

char a[30];

printf("Enter a string\n");

gets(a);

printf("Your inputted string is %s",a);


}

7. main()

char a[30],i;

printf("Enter a string\n");

gets(a);

for(i=0;a[i];i++);

printf("Length of string is %d",i);

8. main()

char a[30],i,j,l,c;

printf("Enter a string\n");

gets(a);

for(i=0;a[i];i++);

for(l=0,j=i-1;l<i/2;l++,j--){

c= a[l];

a[l]=a[j];

a[j]=c;

printf("Reverse string is %s",a);

}
9.

) main()
{
char a[30];
int i,c=0;
printf("Enter a string\n");
gets(a);
for(i=0;a[i];i++)
if(a[i]==' ')
c++;
printf("Words in your string is %d",c+1);
}

10. main()

char a[30],b[30];

int i;

printf("Enter first string\n");

gets(a);

printf("Enter second string\n");


gets(b);

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

if(a[i]<b[i])

printf("%s comes first\n",a);

else if(a[i]>b[i])

printf("%s comes first\n",b);

else

printf("Both are same");

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy