0% found this document useful (0 votes)
43 views

Solution:: 1.write A Function That Returns The Number of Times A Value (Key) Appears in Array

This document contains 6 code snippets that provide solutions to string manipulation problems in C programming. The snippets include functions to find the length of a string, search for a character in a string, count vowels and consonants, reverse a string, compare two strings, and replace a character in a string. Each snippet includes the code for the function as well as a main function to test it.
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)
43 views

Solution:: 1.write A Function That Returns The Number of Times A Value (Key) Appears in Array

This document contains 6 code snippets that provide solutions to string manipulation problems in C programming. The snippets include functions to find the length of a string, search for a character in a string, count vowels and consonants, reverse a string, compare two strings, and replace a character in a string. Each snippet includes the code for the function as well as a main function to test it.
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/ 12

1.Write a function that returns the number of times a value(key) appears in array.

Solution:

#include <stdio.h>
#include<string.h>
int length(char s[]);
int main()
{
char s[100];
int i =0;
printf("Input a string\n");
gets(s);
while (s[i]!='\0')
i++;
printf("Length of the string: %d\n",i);
return 0;
}
int length(char s[])
{
int i =0;
while (s[i] !='\0')
i++;
return i;
}
2.Write a program that computes the sum of a specific column (provided by the user as input) in a 2D
array.
Solution:
#include <stdio.h>
void search(char s[],char k);
int main()
{
char s[100];
char k;
printf("Input a string\n");
gets(s);
printf("Enter the key:\n");
scanf("%c",&k);
search(s,k);
return 0;
}
void search(char s[],char k)
{
int i,f=0;
for(i=0;s[i]!='\0';i++)
{
if(s[i]==k)
{
f=1;
}
}
if(f==1)
{
printf("found");
}
else
{
printf("not found");
}
}
3.Write a program that computes the sum of a specific column (provided by the user as input) in a 2D
array.
Solution:

#include<stdio.h>
#include<string.h>
void count(char arr[]);
int main()
{
char arr[100];
printf("Enter a string\n");
gets(arr);
count(arr);
return 0;
}
void count(char arr[])
{
int v=0,c=0;
int i,len;
len=strlen(arr);
for(i=0;i<len;i++)
{
if((arr[i]>='a' && arr [i]<='z')||(arr[i]>='A' && arr[i]<='Z'))
{
if (arr[i] == 'a' || arr[i] == 'e' || arr[i] == 'i' || arr[i] == 'o' || arr[i] == 'u'|| arr[i] == 'A' || arr[i] ==
'E' || arr[i] == 'I' || arr[i] == 'O' || arr[i] == 'U')
v++;
else
c++;
}
}
printf("Number of Vowel is: %d and Consonant is: %d",v,c);
}
4.Write a program that computes the sum of a specific column (provided by the user as input) in a 2D
array.
Solution:

#include <stdio.h>
#include <string.h>
void reverse(char arr[]);
int main()
{
char arr[100];
printf("Enter a String\n");
gets(arr);
reverse(arr);
return 0;
}
void reverse(char arr[])
{
int len,i,temp;
len=strlen(arr)-1;
for(i=0;i<strlen(arr)/2;i++)
{
temp=arr[i];
arr[i]=arr[len];
arr[len--]=temp;
}
printf("The String after Reversing:%s",arr);
}
5.Write a program that computes the sum of a specific column (provided by the user as input) in a 2D
array.
Solution:

#include <stdio.h>
int compare(char arr1[], char arr2[]);
int main()
{
char a[100],b[100];
printf("Input a string\n");
gets(a);
printf("Input a string\n");
gets(b);
printf("%d",compare(a,b));
return 0;
}
int compare(char arr1[], char arr2[])
{
int c;
for(c=0;arr1[c]!='\0'||arr2[c]!='\0';c++)
{
if(arr1[c]==arr2[c])
{
continue;
}
else if (arr1[c]<arr2[c])
{
return -1;
}
else
{
return 1;
}
}
return 0;
}
6.Write a program that computes the sum of a specific column (provided by the user as input) in a 2D
array.
Solution:

#include<stdio.h>
#include<string.h>
void reverse(char arr[],char oldchar,char newchar);
int main()
{
char arr[100];
char oldchar,newchar;
printf("Enter A String:\n");
gets(arr);
printf("Enter a Character You want to Replace:\n");
scanf("%c",&oldchar);
getchar();
printf("Enter a Replacement:\n");
scanf("%c",&newchar);
reverse(arr,oldchar,newchar);
return 0;
}
void reverse(char arr[],char oldchar,char newchar)
{
int i;
for(i=0;i<strlen(arr);i++)
{
if(arr[i]==oldchar)
{
arr[i]=newchar;
}
}
printf("The New String is %s",arr);
}
CSE 115 LAB ASSIGNMENT-04

Name: Mritunjoy Chakrobarty


ID:1911943042
Section:07

You might also like