Solving assignments is our primary task but due to hectic schedule we are not able to discuss it, so the objective of this form is to post doubts, best possible solutions and discussions on assignments.
Assignment-3.1 Ques 9: Using if else statement ,write a program that reads input up to #,replaces each period with an exclamation mark ,replaces each exclamation mark initially present with two exclamation marks, and reports at the end number of substitutuons it has made.
My approach till now:
#include<stdio.h>
int main() { char a[1024]; int i,j,k; printf("Enter the line:\n"); j=k=0; gets(a); for(i=0;i<1024;i++) { if(a[i]=='#') break; if(a[i]=='.') { a[i]='!'; j++; } /* if(a[i]=='!') { not able to interchange ! }*/ k++; } for(i=0;i<k;i++) { printf("%c",a[i]); } printf("No. of substitutions:%d\n",j); return 0; } I am unable to inter change '!' with "!!" or '!' '!'.
put a condition to stop the control from getting to 3rd if when the 2nd if statement is true..better to use else if here.
to replace '!' with '!!' we need an extra array space just next to the '!',hence when an '!' is encountered we can shift the nex elements to one space difference,thus creating a new element space..
write '!' to this space.
increment the count of j.
Shifting of a part of an array can be done similar to shifting done in insertion sort technique by using a reverse looping statement.