6
Computer Science Practice Assignment S.NO. Sub Par t Question Marks Q1. (a) What will be the output of the following program: #include <iostream.h> void Secret(char Str[ ]) { for (int L=0;Str[L]!='\ 0';L++); for (int C=0;C<L/2;C++) if (Str[C]=='A' || Str[C]=='E') Str[C]=Str[L-C-1]; else { char Temp=Str[C]; Str[C]=Str[L-C-1]; Str[L-C-1]=Temp; } } void main() { char Message[ ]="PreboardExam"; Secret(Message); cout<<Message<<endl; } Ans.: maxEdraoberP (3) (b) void funnystr(char *s, int n = 2) { int i = n; while(i < strlen(s)) { s[i] = '-'; i = i + n; } i = 0; while(s[i] != '\0') { if(s[i] > 'A' && s[i] < 'P') s[i] = tolower(s[i]); (3) 1

CS Practice String Output

Embed Size (px)

Citation preview

Page 1: CS Practice String Output

Computer Science Practice Assignment

S.NO. Sub Part

Question Marks

Q1. (a) What will be the output of the following program: #include <iostream.h> void Secret(char Str[ ]) { for (int L=0;Str[L]!='\0';L++);

for (int C=0;C<L/2;C++)if (Str[C]=='A' || Str[C]=='E')

Str[C]=Str[L-C-1];else{ char Temp=Str[C];

Str[C]=Str[L-C-1];Str[L-C-1]=Temp;

} }void main(){ char Message[ ]="PreboardExam";

Secret(Message);cout<<Message<<endl;

}Ans.: maxEdraoberP

(3)

(b) void funnystr(char *s, int n = 2) { int i = n;

while(i < strlen(s)){

s[i] = '-';i = i + n;

}i = 0;while(s[i] != '\0'){ if(s[i] > 'A' && s[i] < 'P')

s[i] = tolower(s[i]);else if(s[i] > 'a' && s[i] < 'p'){

if(i % 3 == 0)s[i] = tolower(s[i-1]);

elses[i] = tolower(s[i]);

}i++;

} }void main(){ char str[] = "MiCroSoFT";

funnystr(str,3);

(3)

1

Page 2: CS Practice String Output

cout<<str;}

Ans.: mic-oS-fT(c) Give the output of the following program:

#includevoid ReCode ( char Text[ ], int Num );void main ( ){char Note [ ] = "Butterfly";Recode (Note, 2);cout << Note <<endl;}void ReCode (char Text [ ], int Num){for ( int K = 0 ; Text [K] !='\0' ; K++)if ( K % 2 == 0) Text [K] = Text [K]- Num;else if ( islower (Text[K] )) Text [K] = toupper ( Text [K] ) else Text[K] = Text[K] + Num;}

Ans.: @UrTcRdLw

3

(d) What will be the output of the following program: #include<iostream.h>

#include<string.h>void main( ){ char Mes1[ ]=”HuB”, Mes2[ ]=”ThaT”, Mes3[ ]=”BeSt”;int L1=strlen(Mes1), L2=strlen(Mes2), L3=strlen(Mes3);int N=L1+L2+L3;for(int c=0; c<N; c++){

if(c%4==0){

cout<<Mes2[L2-1];L2--;

}else

if(c%3==0){

cout<<Mes1[L1-1];L1--;

}else{

cout<<Mes3[L3-1];L3--;

}}}

Ans.: TtSBaeuBhH

3

2

Page 3: CS Practice String Output

(e) Find the output of the following program (Assuming that all required header files are included)

void main( ){

char NAME[ ] = “admiNStrAtiOn”; for( int x=0;x<strlen(NAME);x++) { if(islower(NAME[x]))

NAME[x] = toupper(NAME[x]);else

if(isupper (NAME[x]))if(x%2==0)

NAME[x] = NAME[x -1];else

NAME[x]--;cout<<NAME <<endl;

}}

Ans.: ADMIIRTRRTINN

3

(f) Find the output of the following program:

#include<iostream.h>#include<ctype.h>void Secret(char Msg[ ], int N);void main( ){ char SMS[ ]=”rEPorTmE”;Secret(SMS,2);cout<<SMS<<endl;}void Secret(char Msg[ ], int N){ for(int C=0;Msg[C]!=’\0’;C++) if(C%2= =0) Msg[C]=Msg[C]+N; else if(isupper(Msg[C])) Msg[C]=tolower(Msg[C]); else Msg[C]=Msg[C]-N;}

Ans . teRmttoe

(g) Find the output of the following program: (Assuming all header files are included)

void Mycode(char Msg[],char C){ for(int i=0;Msg[i]!=’\0’;i++) { if(Msg[i]>=’B’ && Msg[i]<=’H’) Msg[i]=tolower(Msg[i]); else if(Msg[i]==’A’ || Msg[i]==’a’) Msg[i]=C; else

3

Page 4: CS Practice String Output

if(i%2==0) Msg[i]=toupper(Msg[i]); else Msg[i]=Msg[i-1]; } }void main() { char text[]=”ApaCHeSeRvEr”; Mycode(Text,’#’); cout<<” New Line = “<<text;}

Ans.: ###chhSSRRee(h) Find the output of the following program:

#include<iostream.h>#include <string.h>#include <ctype.h>void Convert (char Str[ ], int Len){

for (int Count =0; Count < Len; Count++){

if(isupper(Str[Count+])) Str[Count] = tolower(Str[Count]);else if (islower(Str[Count])) Str[Count] = toupper(Str[Count]); else if (isdigit(Str[Count])) Str[Count] = Str[Count] + 1; else Str[Count] = ‘*’;

}}void main ( ){

char Text[] = “CBSE Exam 2012”;int Size = strlen(Text);Convert (Text, Size);cout << Text << endl;for(int C =0, R = Size -1; C< = Size/2; C++, R- - ){

char Temp = Text[C];Text[C] = Text[R];Text[R] = Temp;

}cout << Text <<endl;

}Ans.: CbSe *xAm*21133112*mxA* eSbC

3

(i) Find the output of the following program: #include <iostream.h>

void Secret(char Str[ ]){for (int L=0;Str[L]!='\0';L++);for (int C=0;C<L/2;C++) if (Str[C]=='A' || Str[C]=='E') Str[C]='#';

2

4

Page 5: CS Practice String Output

else { char Temp=Str[C]; Str[C]=Str[L-C-1]; Str[L-C-1]=Temp; }}void main( ){char Message[ ]="SteveJobs";Secret(Message);cout<<Message<<endl;}

Ans.: sboJevetS

(j) Find the output of the following program;

#include<iostream.h>#include<ctype.h> void main( ){ char TEXT1[ ] = “Cbse@Board!”; for(int I=0; TEXT1 [I]!=’\0’;I++) { if(!isalpha(TEXT1[I])) TEXT1[I]=’*’; else if(isupper(TEXT1 [I])) TEXT1[I]=TEXT1[I]+1; else TEXT1[I] = TEXT1[I+1]; } cout<<TEXT1;}

Ans. : Dse@*Card!*

2

5