0% found this document useful (0 votes)
8 views5 pages

Assignment 8

The document provides a series of programming assignments related to C language, covering topics such as input handling with scanf() and gets(), string declaration and initialization, and various string manipulation tasks. It includes example C programs for counting characters, capitalizing strings, counting vowels, removing whitespace, and more. Each assignment is presented with a question followed by an answer, demonstrating practical coding skills and concepts.

Uploaded by

chauhanyuvansh26
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)
8 views5 pages

Assignment 8

The document provides a series of programming assignments related to C language, covering topics such as input handling with scanf() and gets(), string declaration and initialization, and various string manipulation tasks. It includes example C programs for counting characters, capitalizing strings, counting vowels, removing whitespace, and more. Each assignment is presented with a question followed by an answer, demonstrating practical coding skills and concepts.

Uploaded by

chauhanyuvansh26
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/ 5

Assignment 8

1. What is the difference between scanf() with %s and gets( )?

Ans.

scanf(“%s”) function gets() function

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.

In scanf(“%s”) function white-space is not On other hand in get() function white-space is


considered as input character and also it considered as input character and also it does
stops reading input from external source if stops reading input from external source if any
any white-space is encountered in between. white-space encountered in between it continue
its reading from input source.

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.

2. Define string. How string is declared and initialized?

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.

char first_name[15]; Declaration of a string variable


Initialization of String
char first_name[15] = "ANTHONY";
char first_name[15] = {'A','N','T','H','O','N','Y','\0'};

3. What is the difference between character array and string?

Ans.

String Character array

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.

Ans. #include <stdio.h>

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.

Ans. #include <string.h>

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;
}

6. Write a C program that will capitalize all the letters of a string.

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.

Ans #include <stdio.h>

int main(){

char strArr[100];

int count_a = 0, count_e = 0, count_i = 0, count_o = 0, count_u = 0, s;

printf("Enter a string : ");

fgets(strArr, 100, stdin);

int i = 0;

while (strArr[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++;

} printf("Total no. of vowels = %d", s);

printf("\nCount of A : %d\nCount of E : %d\nCount of I : %d\nCount of O : %d\nCount of U : %d\n",


count_a, count_e, count_i, count_o, count_u);

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;
}

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