Assignment 8
Assignment 8
Ans.
The scanf(“%s”) function can read input from On other hand gets() function is used to receive
keyboard and stores them according to the input from the keyboard till it encounters a
given format specifier. It reads the input till newline or EOF. The whitespace is considered as a
encountering a whitespace, newline or EOF. part of the input.
scanf(“%s”) function takes the format string On other hand get() function takes the name of
and list of addresses of variables. e.g. the variable to store the received value. e.g.
scanf(“%d”, &number); gets(name);
scanf(“%s”) function can read multiple However on other hand get() function will only get
values of different data types. character string data.
Ans. A String is a collection of characters in a linear sequence. . A string is represented using double
quote marks. ‘C’ always treats a string a single data even though it contains whitespaces.
Ans.
String is implemented to store sequence Character Array on the other hand is a sequential
of characters and to be represented as a collection of data type char where each element is
single data type and single entity. a separate entity.
String internal implementation makes it Character array is mutable in nature on the other
immutable in nature. hands.
As String is a class so is provided with No built in functions are provided in Java for
various built-in function substring(), operations on Character Arrays.
charAt() etc.
String can be concatenated either by Character array could not use either of these
using + operator or using its built in function/operator to get concatenated.
function concate().
4. Write a C Program to find length of a string without using library function/s.
int main(){
char str[100];
int i,length=0;
printf("Enter a string: \n");
scanf("%s",str);
for(i=0; str[i]!=’\0’; i++){
length++;
}
printf("\nLength of input string: %d",length);
return 0;
}
5. Write a C program to read a text and count all the occurrences of a particular letter given
by the user.
int main(){
char s[1000],c;
int i,count=0;
printf("Enter the string : ");
gets(s);
printf("Enter character to be searched: ");
c=getchar();
for(i=0;s[i];i++)
{
if(s[i]==c)
{
count++;
}}
printf("character '%c' occurs %d times \n ",c,count);
return 0;
}
Ans. #include<stdio.h>
#include<ctype.h>
int main(){
int ctr=0;
char str_char;
char str[100];
printf("\n Convert a string to uppercase. :\n");
printf("-----------------------------------");
printf("\n Input a string in lowercase : ");
fgets(str, sizeof str, stdin);
printf(" Here is the above string in UPPERCASE :\n ");
while (str[ctr]){
str_char=str[ctr];
putchar (toupper(str_char));
ctr++;
}
printf("\n\n");
return 0;
}
7. Write a C program that counts the total numbers of vowels and their frequency.
int main(){
char strArr[100];
int i = 0;
switch (strArr[i]){
case 'a':
count_a++;
break;
case 'A':
count_a++;
break;
case 'e':
count_e++;
break;
case 'E':
count_e++;
break;
case 'i':
count_i++;
break;
case 'I':
count_i++;
break;
case 'o':
count_o++;
break;
case 'O':
count_o++;
break;
case 'u':
count_u++;
break;
case 'U':
count_u++;
break;
s = i;
i++;
return 0;
}
8. Write a C program to remove the white spaces (blank spaces) from a string.
Ans. #include<stdio.h>
#include<string.h>
void main(){
char str[100],c;
int i,j,len;
printf("C Program to remove space from string \n");
printf("enter the string : \n");
scanf("%[^\n]",str);
len = strlen(str);
for(i=0; i<len; i++){
if(str[i] == ' '){
for(j=i; j<len; j++){
str[j] = str[j+1];
}
len--;
i--;
}}
printf("String after removing space: %s", str);
}
9. Write a program to convert each character of a string into the next alphabet and print the
string.
Ans. #include<stdio.h>
#include<conio.h>
int main(){
char ch;
printf("Enter any character: ");
scanf("%c", &ch);
printf("\n");
if(ch>=65 && ch<90)
printf("%c", ch+1);
else if(ch>=97 && ch<122)
printf("%c", ch+1);
else if(ch==90)
printf("%c", 65);
else if(ch==122)
printf("%c", 97);
else
printf("%c", ch);
getch();
return 0;
}
10. Write a program that accepts a word from the user and prints it in the following way.
For example, if the word is COMPUTER, the program will print it as
C
CO
COM
COMP
COMPU
COMPUT
COMPUTE
COMPUTER
Ans. #include<stdio.h>
int main()
{
char str[20];
printf("Enter a string: ");
scanf("%[^\n]",str);
for(int i=0; str[i]!='\0'; i++)
{
for(int j=0; j<=i; j++) {
printf("%c", str[j]);
}
printf("\n");
} return 0;
}