Character Arrays & Strings
Review Questions
RQ-8.1: State whether the following statements are true or false.
(a) When initializing a string variable during its declaration, we must include the null character as part of the string constant, like ‘GOOD\0’.
Answer: False.
(b) The gets function automatically appends the null character at the end of the string read from the keyboard.
Answer: True.
(c) When reading a string with scanf, it automatically inserts the terminating null character.
Answer: True.
(d) String variables cannot be used with the assignment operator.
Answer: True.
(e) We cannot perform arithmetic operations on character variables.
Answer: True.
(f) We can assign a character constant or a character variable to an int type variable.
Answer: False.
(g) The function scanf cannot be used in any way to read a line of text with the white spaces.
Answer: True.
(h) The ASCII character set consists of 128 distinct characters.
Answer: False.
(i) In the ASCII collating sequence, the uppercase letters precede lowercase letters.
Answer: True.
(j) In C, it is illegal to mix character data with numeric data in arithmetic operations.
Answer: False.
(k) The function getchar skips white-space during input.
Answer: False.
(l) In C, strings cannot be initialized at run time.
Answer: False.
(m) The input function gets has one string parameter.
Answer: True.
(n) The function call strcpy (s2, s1); copies string s2 into string s1.
Answer: False.
(o) The function call strcmp ( ‘abc’ , ‘ABC’ ); returns a positive number.
Answer: True.
Question-8.2 Fill in the blanks in the following statements.
(a) We can use the conversion specification ….. in scanf to read a line of text.
Answer: code.
(b) We can initialize a string using the string manipulation function ……...
Answer:
(c) The function strncat has (three) parameters.
Answer: three
(d) To use the function atoi in a program, we must include the header file ……. .
Answer:
(e) The function ……. does not require any conversion specification to read a string from the keyboard.
Answer: gets.
(f) The function ……… is used to determine the length of a string.
Ans: strlen.
(g) The ……….string manipulation function determines if a character is contained in a string.
Answer: strstr.
(h) The function ………..is used to sort the strings in alphabetical order.
Answer: ASCII.
(i) The function call strcat (s2,s1); appends …… to …….
Answer: One, another.
(j) The printf may be replaced by ……… function for printing trings.
Answer: Puts.
Question-8.3: Describe the limitations of using getchar and scanf functions for reading strings.
Answer:
By using getchar we can read only one character from the keyboard.
We can read only string without white spaces by using scanf function.
Question-8.4: Character strings in C are automatically terminated by the null character.
Explain how this feature helps in string manipulations.
Answer: We know that a string is not a data types in c, but it is consider a data structure stored in array. The string is a variable-length structure and is stored in a fixed-array. therefore, the last element of an array need not be represent at the end.
It is automatically terminate by null character.
Question-8.5: Strings can be assigned values as follows:
(a) During type declaration char string[]={“.........”};
Answer: Read a character string.
(b) Using strcpy function strcpy(string, “......”);
Answer: Copy one string to another.
(c) Reading using scanf function scanf(“%s”,string);
Answer: It takes a string.
(d) Reading using gets function gets(string);
Answer: Read a line of string.
Compare them critically and describe situations where one is superior to others.
Question-8.6: Assuming the variable string contain the value “ The sky is the limit ”, determine
what output of the following segments will be.
(a) printf (“%s”,string);
output:
The sky is the limit
(b) printf(“%25.10s”,string);
output:
The sky is
(c) printf(“%s”,string[0]);
output:
.
(d) for(i=0;string[i]!= “.”,i++)
printf(“%c”,string[i]);
output:
(e) for(i=0;string[i]!= ‘\0’;i++)
printf(“%d\n”,string[i]);
output:
(f) for(i=0;i<=strlen[string]; ;)
{
string[i++]=i;
printf(“%s\n”,string[i]);
}
output:
(g) printf(“%c\n”,string[10]+5);
output:
(h) printf(“%c\n”,string[10]+5’);
output:
Question-8.7: Which of the following statements will correctly store the concatenation of strings
s1 and s2 in string s3?
(a) s3=strcat (s1,s2);
Answer: correct.
(b) strcat(s1,s2,s3);
Answer: error.
(c) strcat(s3,s2,s1);
Answer: error.
(d) strcpy(s3,strcat(s1,s2));
Answer: correct.
(e) strcmp(s3,strcat(s1,s2));
Answer: correct.
(f) strcpy(strcat(s1,s2),s3);
Answer: error.
Question-8.8: What will be the output of the following statements?
printf(“%d”,strcmp(“push”, “pull”));
output:
7
Question-8.9: Assume that s1, s2 and s3 are declared as follows:
char s1[10]= “he”, s2[20]= “she”, s3[30], s4[30];
What will be the output of following statements executed in sequence?
printf(“%s”, strcpy(s3, s1));
printf(“%s”, strcat(strcat(strcpy(s4,s1),“or”),s2));
printf(“%d %d”,strlen(s2)+strlen(s3),strlen(s4));
output:
he
heorshe
5 7
Question-8.10: Find errors if any, in the following code segments;
(a) char str[10]
strcpy(str, “GOD”, 3);
printf(“%s”,str);
Answer: error.
Correct answer: char str[10];
strcpy(str, “GOD”, 3);
printf(“%s”,str);
(b) char str[10];
strcpy(str, “Balagurusamy”);
Answer: no error.
(c) if strstr(“balagurusamy”, “guru”)==0);
printf(“Substring is found”);
Answer: no error.sss
(d) char s1[5], s2[10],
gets(s1, s2);
Answer: error.
Correct answer: char s1[5], s2[10];
gets(s1);
gets(s2);
Question-8.11: What will be the output of the following segment ?
char s1[]= “Kolkata”;
char s2[]= “Pune”;
strcpy(s1, s2);
printf(“%s”,s);
output:
Pune
Question-8.12: What will be the output of the following segment s?
char s1[]= “NEW DELHI”
char s2[]= “BANGALORE”
strcpy(s1, s2, 3);
printf(“%s”, s1);
output:
BAN DELHI
Question-8.13: What will be the output of the following code?
char s1[]= “Jabalpur”
char s2[]= “Jaipur”
printf(strncmp(s1, s2, 2))
output:
0
Question-8.14: What will be the output of the following code?
char s1[]= “ANIL KUMAR GUPTA”
char s2[]= “KUMAR”
printf(strstr(s1,s2));
output:
KUMAR GUPTA
Question-8.15: Compare the working of the following functions:
(a) stcpy and strncpy;
(b) Answer: The function strcpy copies one string to another string but the function strncpy copies only the left-most n characters of the source string to the target string
variable.
(b) strcmp and strncmp; and
Answer: The strcmp function compares two strings identified by the arguments but
strncmp function compares the left-most n characters of two string .
(c) strcat and strncat
Answer: The function strcat joins two strings together but the function strncat
concanate left-most n characters of target string to source string.
Question: Write a program which reads your name from the keyboard and output a list of ANCII codes, which represent your name.
Answer:
#include
#include
#include
void main()
{
int k,i,l;
char dinar[100];
printf("Enter a STRING:\n");
gets(dinar);
l=strlen(dinar);
printf("the ASCII values of the string are:\n");
for(i=0;i
{
k=dinar[i];
printf("%d ",k);
}
}
Question: Write a program to do the following:
(a) To output the question “Who is the inventor of C?”.
(b) To accept an answer.
(c) To print out “Good” and then stop, if the answer is correct.
To output the massage ‘try again’ if the answer is wrong.
(d)To display the correct answer when the answer is wrong even at the third attempt and stop.
Answer:
#include
#include
void main()
{
char s1[10];
char s2[10]="Atiqur";
int n;
clrscr();
printf("Who is the inventor of C ?");
printf("\nAnswer is:");
scanf("%s",s1);
n=strcmp(s1,s2);
if(n!=0)
printf("try again");
else
printf("Good");
getch();
}
Question: Write a program to extract a portion of a character string and print the extracted string. Assume that m characters are extracted, starting with the nth character.
Answer:
Question: Write a program which will read a text and count all occurances of a particular word.
Answer:
#include
#include
#include
void main()
{
const char* str;
const char c;
int num = 0;
int i;
clrscr();
const int end = strlen(str);
for(i = 0; i < end; ++i)
{
if( str[i] == c )
{
++num;
}
}
getch();
}
Question: Write a program which will read a string and rewrite it in the alphabetical order. For example, the word STRING should be written as GINRST.
Answer:
#include
#include
#include
void main()
{
char a[30],temp;
int n=0,j,i;
clrscr();
printf("Enter the string\n");
gets(a);
while(a[n]!='\0')
{
n++;
}
for(i=0;i
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("The string in alphabetical order is\n");
for(i=0;i
Question: Write a program to replace a particular word by another word in a given string. For example, the word “PASCAL” should be replaced by “C” in the text “It is good to program in PASCAL language”.
Answer:
#include
#include
int stlen(char str[50])
{
int len = 0;
while(str[len]!='\0')
len++;
len;
return len;
}
void stcat(char str1[50], char str2[50])
{
int i = 0,len = 0;
while(str1[len]!='\0')
len++;
while(str2[i]!='\0')
{
str1[len] = str2[i];
i++;
len++;
}
str1[len] = '\0';
}
void main()
{
char str1[50], str2[50], str3[50], temp[50];
int len1, len2, len3, i, j, match, k;
clrscr();
printf("\n\n\t ENTER A SENTENCE: ");
gets(str1);
len1 = stlen(str1);
printf("\n\n\t ENTER A STRING WHICH YOU WANT TO DELETE:");
gets(str2);
len2 = stlen(str2);
printf("\n\n\t ENTER A NEW STRING WHICH YOU WANT TO INSERT : ");
gets(str3);
len3 = stlen(str3);
for(i=0;i<=len1;i++)
{
match = 1;
for(j=0;j<=len2;j++)
if(str2[j]!=str1[i+j])
{
match = 0;
break;
}
if(match)
{
for(k=0,j=i+len2+1;j<=len1;j++,k++)
temp[k] = str1[j];
temp[k] = '\0';
for(j=0;j<=len3;j++)
str1[i+j] = str3[j];
str1[i+j] = '\0';
stcat(str1,temp);
len1 = len1+len2+len3;
i = i + j;
}
}
printf("\n\n\t OUTPUT IS:" );
puts(str1);
getch();
}
Question: A Maruti car dealer maintains a record of sales of various vehicles in the following form:
Vehicle type Month of sales price
MARUTI-800 02/01 210000
MARUTI-DX 07/01 265000
GYPSY 04/02 315750
MARUTI-VAN 08/02 240000
Write a program to read this data into a table of strings and output the details of a particular vehicles sold during a specified period. The program should request the user to input the vehicle type and the period (starting month, ending month).
Question: Write a program that reads a string from the keyboard and determine whether the string is a palindrome or not. (A string is a palindrome if it can be read from left and right with the same meaning. For example, Madam and Anna are palindrome string. Ignore capitalization).
Answer:
#include
#include
void main()
{
int pelin(char[]);
int flag;
char str[30];
clrscr();
printf("*****PELINDROMESTRING*****\n\n");
printf("Enter string : ");
scanf("%s",str);
if(pelin(str))
printf("\nSTRING IS PELINDROME");
else
printf("\nSTRING IS NOT PELINDROME");
getch();
}
int pelin(char str[]){
int flag=0,i,j;
char rev[30];
for(i=0;str[i]!='\0';i++)
rev[i] = str[i];
//checking whether pelindrome
i--;
for(j=0;i>=0;j++,i--){
if(rev[j]!=str[i]){
flag=0;
return(flag);
}
}
flag=1;
return (flag);
}
Question: Write a program that reads the cost of an item in the form RRRR.PP (Where RRRR denotes Rupees and PP denotes paisa) and converts the value to a string of words that expresses the numerical value in words. For example, if we input 125.75, the output should be “ONE HUNDRED TWENTY FIVE AND PAISA SEVENTY FIVE”.
Answer:
#include
#include
#include
void main(){
char init[27][12] = {" one "," two "," three ",
" four "," five "," six ",
" seven "," eight "," nine ",
" ten "," eleven "," twelve ",
" thirteen "," fourteen "," fifteen ",
" sixteen "," seventeen "," eighteen ",
" nineteen "," twenty "," thirty ",
" fourty "," fifty "," sixty ",
" seventy "," eighty "," ninty "};
char sthou[20]="",shund[20]="",sval1[20]="",sval2[20]="",result[100]="";
int thou=0,hund=0,ten=0,temp=0,val1,val2,num,rem,c=0;
//USING COBOL LOGIC by dinar
printf("*****AMOUNT IN WORDS*****\n\n");
printf("Enter any value (upto 4 digits) : ");
scanf("%d",&num);
while(num>0){
rem = num%10;
c++;
if(c<=2)
temp = temp * 10 +rem;
else if(c==3)
hund=rem;
else if(c==4)
thou=rem;
num=num/10;
}
while(temp>0){ //as ten contains two digit so reverse it
rem = temp%10;
ten = ten * 10 + rem;
temp= temp/10;
}
if(thou>0){
strcpy(sthou,init[thou-1]);
strcat(sthou," thousand ");
strcat(result,sthou);
}
if(hund>0){
strcpy(shund,init[hund-1]);
strcat(shund," hundred ");
strcat(result,shund);
}
if(ten>0){
if(ten>20){
val1 = ten/10;
val2 = ten%10;
}
if(val1>0){
strcpy(sval1,init[val1+(18-1)]);
strcat(result,sval1);
}
if(val2>0){
strcpy(sval2,init[val2-1]);
strcat(result,sval2);
}
}
printf("\n\nAmount in word is as under \n");
printf("%s",result);
getch();
}
Question: Develop a program that will read and store the details of a list of students in the format
Roll No. Name Marks obtaine d
………….. ………… ………………..
………….. ………… ………………..
………….. …………. …………………
………….. ……….. …………………
And produce the following output lits:
(a) Alphabetical list of names, roll numbers and marks obtained.
(b) List sorted on roll numbers.
(c) List sorted on marks (rank-wise list).
Question:
Write a program to read strings and compare them using the function strncmp() and print the massage that the first string is equal, less or greater than the second one.
Answer:
#include
#include
#include
void main()
{
char s1[20],s2[20];
int x;
printf("\n\nEnter two string constants\n");
printf("?");
scanf("%s %s",s1,s2);
x=strcmp(s1,s2);
if(x==0)
{
printf("\n\nStrings are equal\n");
}
else if(x<0)
{
printf("String 1 is less than string 2");
}
else
{
printf("String 1 is greater than String 2");
}
getch();
}
Question: Write a program to read a line of text from the keyboard and print out the number of occurrences of a given substring using the function strstr().
Answer:
#include
#include
#include
void main()
{
char a[100],b[100];
char n,sazon;
printf("Input two string:\n");
gets(a);
n=strlen(a);
gets(b);
sazon=strncmp(a,b,n);
if(dinar==0)
printf("equal.");
else
if(sazon>0)
printf("a>b");
else
printf("a
getch();
}
Question: Write a program that will copy m consecutive characters from a string s1 beginning at position n into another string s2.
Answer:
#include
#include
#include
void main()
{
char str1[20],str2[20],ch;
int i;
clrscr();
printf("*****Strings Copy Function*****\n\n");
printf("Enter string1: ");
scanf("%s",str1);
printf("\n\nUsing Inbuilt Function");
strcpy(str2,str1);
printf("\nString1 : %s\nString2 : %s\n",str1,str2);
strcpy(str2,""); //empty to copy again
printf("\n\nWithout Using Inbuilt Function");
for(i=0;str1[i]!='\0';i++)
str2[i] = str1[i];
str2[i]='\0';
printf("\nString1 : %s\nString2 : %s\n",str1,str2);
getch();
}
Question: Write a program to create a directory of students with roll numbers. The program should display the roll number for a specified name and vice-verse.
Question: Given that
char str[ ]= “123456789”;
Write a program that displays the following:
1
232
34543
4567654
567898765
Answer:
#include
#include
void main()
{
int i,j,k,m,num,c,tmp;
clrscr();
printf("*****PYRAMID*****\n\n");
printf("Enter num of lines : ");
scanf("%d",&num);
for(i=1;i<=num;i++){
c=i;
for(k=1;k<=(num-i);k++)
putchar(' ');
for(j=i;j>=1;j--){
if(c>9){
printf("%d",0);
tmp=c;
}
else{
printf("%d",c++);
tmp=c;
}
}
tmp--;
for(m=tmp;m>i;)
printf("%d",--m);
printf("\n");
}
getch();
}
thank you so much!
ReplyDeleteyour program is not bad but some error
ReplyDelete