Chapter: Puppetting on string | Let us C solution with details description and tutorials | yashwant kanetkar
[D] Attempt the following:
(a) Which is more appropriate for reading in a multi-word string? gets( ) printf( ) scanf( ) puts( )
Show Solutions
Hide Solutions
Chapter: Puppetting On Strings
/*
[D] Attempt the following:
(a) Which is more appropriate for reading in a multi-word string? gets( ) printf( ) scanf( ) puts( )
*/
#include <stdio.h> //header file
#include <conio.h> //header file
int main() //main function
{
// Answer: Option A gets()
char string[40];
printf("Enter a string:");
gets(string);
printf("The string input was: %s\n", string);
getch();
return 0; // return int value
}
(b) If the string "Alice in wonder land" is fed to the following scanf( ) statement, what will be the contents of the arrays str1, str2, str3 and str4? scanf ( "%s%s%s%s%s", str1, str2, str3, str4 ) ;
Show Solutions
Hide Solutions
Chapter: Puppetting On Strings
/* 4. (b) If the string "Alice in wonder land" is fed to the following
scanf( ) statement, what will be the contents of the arrays str1, str2, str3
and str4? scanf ( "%s%s%s%s%s", str1, str2, str3, str4 ) ;
*/
#include <stdio.h> //header file
#include <conio.h> //header file
int main() //main function
{
char str1[7], str2[7], str3[7], str4[7];
scanf("%s%s%s%s", &str1,&str2,&str3,&str4 ) ;
printf("%s %s %s %s",str1, str2, str3, str4);
// Answer is: str1=Alice , str2=in ,str3=wonder, str4=land
getch();
return 0; // return int value
}
(c) Write a program that converts all lowercase characters in a given string to its equivalent uppercase character.
Show Solutions
Hide Solutions
Chapter: Puppetting On Strings
/* d (c) Write a program that converts all lowercase characters in a given string
to its equivalent uppercase character.
*/
#include <stdio.h> //header file
#include <conio.h> //header file
int main() //main function
{
char word[30];
int i=0;
printf("\n Type a word with capital and small letters. ");
scanf("%s",word);
while(word[i]!='\0') // \0 means last char in array.
{
if(word[i]>=97&&word[i]<=122)
word[i]=word[i]-32;
i++;
}
printf("\n The word in Uppercase is %s",word);
getch();
return 0; // return int value
}
(d) Write a program that extracts part of the given string from the specified position. For example, if the sting is "Working with strings is fun", then if from position 4, 4 characters are to be extracted then the program should return string as "king". Moreover, if the position from where the string is to be extracted is given and the number of characters to be extracted is 0 then the program should extract entire string from the specified position.
Show Solutions
Hide Solutions
Chapter: Puppetting On Strings
/* d (d) Write a program that extracts part of the given string from the
specified position. For example, if the sting is "Working with
strings is fun", then if from position 4, 4 characters are to be
extracted then the program should return string as "king".
Moreover, if the position from where the string is to be extracted
is given and the number of characters to be extracted is 0 then the
program should extract entire string from the specified position.
*/
#include <stdio.h> //header file
#include <conio.h> //header file
int main() //main function
{
char stri[]="Working with string is fun";
char stri2[10];
int a,flag1=0,i=0,b;
printf("\n Enter a position for sentence: Working with string is fun: ");
scanf("%d",&a);
while(flag1==0)
{
if(i==(a-1))
{
if(stri[i]==32)
{
i++;
for(b=0;;b++,i++)
{
if(stri[i]==32||stri[i]=='\0')
{
stri2[b]='\0';
break;
}
stri2[b]=stri[i];
}
}
else
for(b=0;;b++,i++)
{
if(stri[i]==32||stri[i]=='\0')
{
stri2[b]='\0';
break;
}
stri2[b]=stri[i];
}
flag1=1;
}
i++;
}
printf("\n%s",stri2);
getch();
return 0; // return int value
}
(e) Write a program that converts a string like "124" to an integer 124.
Show Solutions
Hide Solutions
Chapter: Puppetting On Strings
/*d (e) Write a program that converts a string like "124" to an integer 124.
*/
#include <stdio.h> //header file
#include <conio.h> //header file
int main() //main function
{
char string[]="123";
int integer=atoi(string); // by use of functions
printf("%d\n", integer);
getch();
return 0; // return int value
}
(f) Write a program that replaces two or more consecutive blanks in a string by a single blank. For example, if the input is Grim return to the planet of apes!! the output should be Grim return to the planet of apes!!
Show Solutions
Hide Solutions
Contents goes here...
Two-dimensional array, Array of pointers to strings
[E] Answer the following:
E (a) How many bytes in memory would be occupied by the following array of pointers to strings? How many bytes would be required to store the same strings, if they are stored in a two-dimensional character array? char *mess[ ] = { "Hammer and tongs", "Tooth and nail", "Spit and polish", "You and C" } ;
Show Solutions
Hide Solutions
Chapter: Puppetting On Strings
/* [E] Answer the following:
E (a) How many bytes in memory would be occupied by the following array
of pointers to strings? How many bytes would be required to store the
same strings, if they are stored in a two-dimensional character array?
char *mess[ ] = { "Hammer and tongs", "Tooth and nail", "Spit and
polish", "You and C" } ;
*/
#include <stdio.h> //header file
#include <conio.h> //header file
int main() //main function
{
char *mess[ ] = { "Hammer and tongs", "Tooth and nail", "Spit and polish", "You and C" } ;
// As pointer is used to hold address, It will occupy as integer occupy.
// in above declaration, address of strings is hold by mess. 4 address of string is held by
// pointer to string
//
getch();
return 0; // return int value
}
E (b) Can an array of pointers to strings be used to collect strings from the keyboard? If not, why not?
Show Solutions
Hide Solutions
/* E (b) Can an array of pointers to strings be used to collect strings
from the keyboard? If not, why not?
*/
#include <stdio.h> //header file
#include <conio.h> //header file
int main() //main function
{
// yes, use the base address to use it.
getch();
return 0; // return int value
}
[F] Attempt the following:
F (a) Write a program that uses an array of pointers to strings str[ ]. Receive two strings str1 and str2 and check if str1 is embedded in any of the strings in str[ ]. If str1 is found, then replace it with str2. char *str[ ] = { "We will teach you how to...", "Move a mountain", "Level a building", "Erase the past", "Make a million", "...all through C!" } ; For example if str1 contains "mountain" and str2 contains "car", then the second string in str should get changed to "Move a car".
Show Solutions
Hide Solutions
/* [F] Attempt the following:
F (a) Write a program that uses an array of pointers to strings str[ ].
Receive two strings str1 and str2 and check if str1 is embedded in any
of the strings in str[ ]. If str1 is found, then replace it with str2.
char *str[ ] = { "We will teach you how to...", "Move a mountain", "Level a building",
"Erase the past", "Make a million", "...all through C!" } ; For example if str1 contains
"mountain" and str2 contains "car", then the second string in str should get changed to
"Move a car".
*/
#include <stdio.h> //header file
#include <conio.h> //header file
int main() //main function
{
// refer to this image to view solution
// https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhbpsGfv_WrrW2oHK5lAtazTUzxRZdlDkhHmySAPBSbJ39q_UqKPJEZcZqUfVHrqrFhKxt9eQW-M4OyaT4ttHkIXuOASA35Ds2W6ozZ3zpmK7hpSFeDVj4iTSNqlvdTOadKkFiRv67XkCRk/s1600-h/9fa.jpg
getch();
return 0; // return int value
}
F (b) Write a program to sort a set of names stored in an array in alphabetical order.
Show Solutions
Hide Solutions
Chapter: Puppetting On Strings
/* F (b) Write a program to sort a set of names stored in an array in alphabetical order.
*/
#include <stdio.h> //header file
#include <conio.h> //header file
int main() //main function
{
char words[10][20];
char temp[30];
char temp2[30];
char clear[1]={'\0'};
int i,j;
for(i=0;i<5;i++)
{
printf("\n Key in the %d word: ",i+1);
scanf("%s",&words[i][0]);
}
for(i=0;i<4;i++)
{
for(j=0;words[i][j]!=0;j++)
{
if(words[i][j]>words[i+1][j])
{
strcpy(temp,&words[i][0]);
strcpy(temp2,&words[i+1][0]);
strcpy(&words[i][0],clear);
strcpy(&words[i+1][0],clear);
strcpy(&words[i][0],temp2);
strcpy(&words[i+1][0],temp);
i=-1;
}
else if (words[i][j]==words[i+1][j])
continue;
else
break;
}
}
for(i=0;i<5;i++)
{
printf("\n %s ",&words[i][0]);
}
getch();
return 0; // return int value
}
F (c) Write a program to reverse the strings stored in the following array of pointers to strings: char *s[ ] = { "To err is human...", "But to really mess things up...", "One needs to know C!!" } ; Hint: Write a function xstrrev ( string ) which should reverse the contents of one string. Call this function for reversing each string stored in s.
Show Solutions
Hide Solutions
Chapter: Puppetting On Strings
/* F (c)
Write a program to reverse the strings stored in the following array of pointers to strings:
char *s[ ] = {
"To err is human...",
"But to really mess things up...",
"One needs to know C!!"
} ;
*/
#include <stdio.h> //header file
#include <conio.h> //header file
int main() //main function
{
char *s[]={
"To err is human...",
"But to really mess things up...",
"One needs to know C!!"
};
int i;
for(i=0;i<3;i++)
xstrrev(s[i]);
for(i=0;i<3;i++)
printf("\n %s ",s[i]);
getch();
return 0; // return int value
}
xstrrev(char *m)
{
char *temp1;
char *temp2;
char *clear;
int i,len,j;
*clear='\0';
len=strlen(m)-1;
for(i=0,j=len;;i++,j--)
{
printf("\n b4 *(m+i) is %c",*(m+i));
*temp1=*(m+i);
*temp2=*(m+j);
*(m+i)=*clear;
*(m+j)=*clear;
*(m+i)=*temp2;
*(m+j)=*temp1;
/* printf("\n i is %d, j is %d",i,j); */
if((j-i)==1||j==i)
break;
printf("\n After *(m+i) is %c",*(m+i));
}
}
F (d) Develop a program that receives the month and year from the keyboard as integers and prints the calendar in the following format.
Note that according to the Gregorian calendar 01/01/1900 was Monday. With this as the base the calendar should be generated.
Show Solutions
Hide Solutions
Chapter: Puppetting On Strings
/* F (d)
Develop a program that receives the month and year from the keyboard as integers and prints the calendar in the following format.
Note that according to the Gregorian calendar 01/01/1900 was Monday. With this as the base the calendar should be generated.
Modify the above program suitably so that once the calendar for a particular month and year has been displayed on the screen,
then using arrow keys the user must be able to change the calendar in the following manner: Up arrow key : Next year, same
month Down arrow key : Previous year, same month Right arrow key : Same year, next month Left arrow key : Same year, previous
month If the escape key is hit then the procedure should stop. Hint: Use the getkey( ) function discussed in Chapter 8,
problem number [L](c)
*/
#include <stdio.h> //header file
#include <conio.h> //header file
#include <dos.h> //header file
int main() //main function
{
int f=0,year,month,mod,i,j,k,m;
char z[1];
clrscr();
while(1)
{
if(f==0)
{ gotoxy(20,2);
printf("\nKey in the year");
scanf("%d",&year);
gotoxy(20,4);
printf("\nKey in the month(digits)");
scanf("%d",&month);
clrscr();
if(year<1900||year>3500)
{
printf("\nCalendar out of range");
continue;
}
if(month>12||month<1)
{
printf("\nInvalid month");
continue;
}
creategrid();
header(year,month);
f=1;
}
if(year<1900)
break;
mod=dayofmonth(year,month);
if(mod==0)
mod=7;
j=daysinmonth(year,month);
for(i=1,k=mod;i<=j;i++,k++)
{
gotoarr(k);
printf("%d",i);
}
m=getkey();
if(m==72)
year=year+1;
if(m==80)
year=year-1;
if(m==75)
month=month-1;
if(m==77)
month=month+1;
if(month==13)
{
month=1;
year=year+1;
}
if(month==0)
{
month=12;
year=year-1;
}
if(year<1900)
break;
creategrid();
header(year,month);
}
getch();
return 0; // return int value
}
creategrid()
{
int i;
for(i=17;i<=59;i++)
{
if(i==17)
{
gotoxy(i,1);
printf("%c",218);
gotoxy(i,22);
printf("%c",192);
}
else if(i==59)
{
gotoxy(i,1);
printf("%c",191);
gotoxy(i,22);
printf("%c",217);
}
else
{
gotoxy(i,1);
printf("%c",196);
gotoxy(i,22);
printf("%c",196);
}
}
for(i=20;i<=56;i++)
{
if(i==20)
{
gotoxy(i,4);
printf("%c",218);
gotoxy(i,21);
printf("%c",192);
}
else if(i==56)
{
gotoxy(i,4);
printf("%c",191);
gotoxy(i,21);
printf("%c",217);
}
else
{
gotoxy(i,4);
printf("%c",196);
gotoxy(i,21);
printf("%c",196);
}
}
for(i=2;i<=21;i++)
{
gotoxy(17,i);
printf("%c",179);
gotoxy(59,i);
printf("%c",179);
}
for(i=5;i<=20;i++)
{
gotoxy(20,i);
printf("%c",179);
gotoxy(56,i);
printf("%c",179);
}
gotoxy(22,7);
printf("Mon");
gotoxy(27,7);
printf("Tue");
gotoxy(32,7);
printf("Wed");
gotoxy(37,7);
printf("Thu");
gotoxy(42,7);
printf("Fri");
gotoxy(47,7);
printf("Sat");
gotoxy(52,7);
printf("Sun");
gotoxy(21,23);
printf("%c=Next year",30);
gotoxy(39,23);
printf("%c=Previous year",31);
gotoxy(21,24);
printf("%c=Next month",16);
gotoxy(39,24);
printf("%c=Previous month",17);
}
getkey()
{
union REGS i,o;
while(!kbhit());
i.h.ah=0;
int86(22,&i,&o);
return(o.h.ah);
}
header(int year,int month)
{
int len;
char *m;
switch(month)
{
case 1:
m="January";
break;
case 2:
m="February";
break;
case 3:
m="March";
break;
case 4:
m="April";
break;
case 5:
m="May";
break;
case 6:
m="June";
break;
case 7:
m="July";
break;
case 8:
m="August";
break;
case 9:
m="September";
break;
case 10:
m="October";
break;
case 11:
m="November";
break;
case 12:
m="December";
break;
}
len=strlen(m)+5;
len=len/2;
gotoxy(38-len,5);
printf("%s %d",m,year);
}
dayofmonth(int year,int month)
{
int leap,mod,check=0,i,diff;
float days;
long days1;
if((year%4==0&&year%100!=0)||year%400==0)
check=1;
year=year-1;
diff=year-1900;
leap=(year-1900)/4-(year-1900)/100+((year/400)-4);
days=((diff-leap)*365.0)+(leap*366.0)+365+1;
for(i=1;i<=12;i++)
{
if(i==month+1)
break;
else if(i==1)
continue;
else if(i==3)
{
if(check==0)
days=days+28;
else if(check==1)
days=days+29;
}
else if(i<9)
{
if(i%2==0)
days=days+31;
else
days=days+30;
}
else if(i==9)
days=days+31;
else if(i>9)
{
if(i%2==0)
days=days+30;
else
days=days+31;
}
}
days1=days;
mod=days1%7;
return(mod);
}
daysinmonth(int year,int month)
{
int days;
if(((year%4==0&&year%100!=0)||year%400==0)&&month==2)
days=29;
else if(month==2)
days=28;
else if(month<8)
{
if(month%2==0)
days=30;
else
days=31;
}
else if(month==8)
days=31;
else if(month>8)
{
if(month%2==0)
days=31;
else
days=30;
}
return(days);
}
gotoarr(int i)
{
int row,col,x,y;
row=((i-1)/7)+1;
if(i<=7)
i=i+7;
col=i%7;
if(col==0)
col=7;
y=7+(2*row);
x=17+(5*col);
gotoxy(x,y);
}
/*
source: http://bbb-letuscsolutions.blogspot.com/2009/10/chap9fe-creating-dynamic-calendar.html
*/
F (e) Modify the above program suitably so that once the calendar for a particular month and year has been displayed on the screen, then using arrow keys the user must be able to change the calendar in the following manner: Up arrow key : Next year, same month Down arrow key : Previous year, same month Right arrow key : Same year, next month Left arrow key : Same year, previous month If the escape key is hit then the procedure should stop. Hint: Use the getkey( ) function discussed in Chapter 8, problem number [L](c).
Show Solutions
Hide Solutions
Chapter: Puppetting On Strings
/* F (e) Modify the above program suitably so that once the calendar for a particular
month and year has been displayed on the screen, then using arrow keys the user
must be able to change the calendar in the following manner: Up arrow key :
Next year, same month Down arrow key : Previous year, same month Right arrow
key : Same year, next month Left arrow key : Same year, previous month If the
escape key is hit then the procedure should stop. Hint: Use the getkey( )
function discussed in Chapter 8, problem number [L](c).
*/
#include <stdio.h> //header file
#include <conio.h> //header file
#include <dos.h> //header file
int main() //main function
{
int f=0,year,month,mod,i,j,k,m;
char z[1];
clrscr();
while(1)
{
if(f==0)
{ gotoxy(20,2);
printf("\nKey in the year");
scanf("%d",&year);
gotoxy(20,4);
printf("\nKey in the month(digits)");
scanf("%d",&month);
clrscr();
if(year<1900||year>3500)
{
printf("\nCalendar out of range");
continue;
}
if(month>12||month<1)
{
printf("\nInvalid month");
continue;
}
creategrid();
header(year,month);
f=1;
}
if(year<1900)
break;
mod=dayofmonth(year,month);
if(mod==0)
mod=7;
j=daysinmonth(year,month);
for(i=1,k=mod;i<=j;i++,k++)
{
gotoarr(k);
printf("%d",i);
}
m=getkey();
if(m==72)
year=year+1;
if(m==80)
year=year-1;
if(m==75)
month=month-1;
if(m==77)
month=month+1;
if(month==13)
{
month=1;
year=year+1;
}
if(month==0)
{
month=12;
year=year-1;
}
if(year<1900)
break;
creategrid();
header(year,month);
}
getch();
return 0; // return int value
}
creategrid()
{
int i;
for(i=17;i<=59;i++)
{
if(i==17)
{
gotoxy(i,1);
printf("%c",218);
gotoxy(i,22);
printf("%c",192);
}
else if(i==59)
{
gotoxy(i,1);
printf("%c",191);
gotoxy(i,22);
printf("%c",217);
}
else
{
gotoxy(i,1);
printf("%c",196);
gotoxy(i,22);
printf("%c",196);
}
}
for(i=20;i<=56;i++)
{
if(i==20)
{
gotoxy(i,4);
printf("%c",218);
gotoxy(i,21);
printf("%c",192);
}
else if(i==56)
{
gotoxy(i,4);
printf("%c",191);
gotoxy(i,21);
printf("%c",217);
}
else
{
gotoxy(i,4);
printf("%c",196);
gotoxy(i,21);
printf("%c",196);
}
}
for(i=2;i<=21;i++)
{
gotoxy(17,i);
printf("%c",179);
gotoxy(59,i);
printf("%c",179);
}
for(i=5;i<=20;i++)
{
gotoxy(20,i);
printf("%c",179);
gotoxy(56,i);
printf("%c",179);
}
gotoxy(22,7);
printf("Mon");
gotoxy(27,7);
printf("Tue");
gotoxy(32,7);
printf("Wed");
gotoxy(37,7);
printf("Thu");
gotoxy(42,7);
printf("Fri");
gotoxy(47,7);
printf("Sat");
gotoxy(52,7);
printf("Sun");
gotoxy(21,23);
printf("%c=Next year",30);
gotoxy(39,23);
printf("%c=Previous year",31);
gotoxy(21,24);
printf("%c=Next month",16);
gotoxy(39,24);
printf("%c=Previous month",17);
}
getkey()
{
union REGS i,o;
while(!kbhit());
i.h.ah=0;
int86(22,&i,&o);
return(o.h.ah);
}
header(int year,int month)
{
int len;
char *m;
switch(month)
{
case 1:
m="January";
break;
case 2:
m="February";
break;
case 3:
m="March";
break;
case 4:
m="April";
break;
case 5:
m="May";
break;
case 6:
m="June";
break;
case 7:
m="July";
break;
case 8:
m="August";
break;
case 9:
m="September";
break;
case 10:
m="October";
break;
case 11:
m="November";
break;
case 12:
m="December";
break;
}
len=strlen(m)+5;
len=len/2;
gotoxy(38-len,5);
printf("%s %d",m,year);
}
dayofmonth(int year,int month)
{
int leap,mod,check=0,i,diff;
float days;
long days1;
if((year%4==0&&year%100!=0)||year%400==0)
check=1;
year=year-1;
diff=year-1900;
leap=(year-1900)/4-(year-1900)/100+((year/400)-4);
days=((diff-leap)*365.0)+(leap*366.0)+365+1;
for(i=1;i<=12;i++)
{
if(i==month+1)
break;
else if(i==1)
continue;
else if(i==3)
{
if(check==0)
days=days+28;
else if(check==1)
days=days+29;
}
else if(i<9)
{
if(i%2==0)
days=days+31;
else
days=days+30;
}
else if(i==9)
days=days+31;
else if(i>9)
{
if(i%2==0)
days=days+30;
else
days=days+31;
}
}
days1=days;
mod=days1%7;
return(mod);
}
daysinmonth(int year,int month)
{
int days;
if(((year%4==0&&year%100!=0)||year%400==0)&&month==2)
days=29;
else if(month==2)
days=28;
else if(month<8)
{
if(month%2==0)
days=30;
else
days=31;
}
else if(month==8)
days=31;
else if(month>8)
{
if(month%2==0)
days=31;
else
days=30;
}
return(days);
}
gotoarr(int i)
{
int row,col,x,y;
row=((i-1)/7)+1;
if(i<=7)
i=i+7;
col=i%7;
if(col==0)
col=7;
y=7+(2*row);
x=17+(5*col);
gotoxy(x,y);
}
/*
source: http://bbb-letuscsolutions.blogspot.com/2009/10/chap9fe-creating-dynamic-calendar.html
*/
F (f) A factory has 3 division and stocks 4 categories of products. An inventory table is updated for each division and for each product as they are received. There are three independent suppliers of products to the factory: (a) Design a data format to represent each transaction. (b) Write a program to take a transaction and update the inventory. (c) If the cost per item is also given write a program to calculate the total inventory values.
Show Solutions
Hide Solutions
Contents goes here...
F (g) A dequeue is an ordered set of elements in which elements may be inserted or retrieved from either end. Using an array simulate a dequeue of characters and the operations retrieve left, retrieve right, insert left, insert right. Exceptional conditions such as dequeue full or empty should be indicated. Two pointers (namely, left and right) are needed in this simulation.
Show Solutions
Hide Solutions
Contents goes here...
F (h) Write a program to delete all vowels from a sentence. Assume that the sentence is not more than 80 characters long.
Show Solutions
Hide Solutions
Chapter: Puppetting On Strings
/* F (h) Write a program to delete all vowels from a sentence. Assume that the
sentence is not more than 80 characters long.
*/
#include <stdio.h> //header file
#include <conio.h> //header file
#include <dos.h> //header file
int main() //main function
{
char sentence[]="She sells seashells on the seashore";
int i;
for(i=0;sentence[i]!=0;i++)
{
if(sentence[i]==97||sentence[i]==101||sentence[i]==105||sentence[i]==111||sentence[i]==117)
{
for(;sentence[i+1]!=0;i++)
sentence[i]=sentence[i+1];
sentence[i]='\0';
i=-1;
}
}
printf("\n%s",sentence);
getch();
return 0; // return int value
}
/*
source: http://bbb-letuscsolutions.blogspot.com/2009/10/chap9fe-creating-dynamic-calendar.html
*/
F (i) Write a program that will read a line and delete from it all occurrences of the word ‘the’.
Show Solutions
Hide Solutions
Chapter: Puppetting On Strings
/* F (i) Write a program that will read a line and delete from it all occurrences of the word ‘the’.
*/
#include <stdio.h> //header file
#include <conio.h> //header file
#include <dos.h> //header file
int main() //main function
{
char sent[]="Write a program that will read the line and delete from it all occurrences of the word the ";
int i;
for(i=0;sent[i]!=0;i++)
{
if(sent[i]==116&&sent[i+1]==104&&sent[i+2]==101&&sent[i+3]==32)
{
for(;sent[i+4]!=0;i++)
sent[i]=sent[i+4];
sent[i]='\0';
i=-1;
}
}
printf("\n%s",sent);
getch();
return 0; // return int value
}
/*
source: http://bbb-letuscsolutions.blogspot.com/2009/10/chap9fe-creating-dynamic-calendar.html
*/
F (j) Write a program that takes a set of names of individuals and abbreviates the first, middle and other names except the last name by their first letter.
Show Solutions
Hide Solutions
Chapter: Puppetting On Strings
/* F (j)Write a program that takes a set of names of individuals and abbreviates the
first, middle and other names except the last name by their first letter.
*/
#include <stdio.h> //header file
#include <conio.h> //header file
#include <dos.h> //header file
int main() //main function
{
char names[5][30]={
"Name with long",
"rock and role",
"david bechham",
"rafel naddal naddl",
"yosu pathan"
};
int i,j,c,k,f,len,a;
for(i=0;i<5;i++)
{
for(j=0;names[i][j]!=0;j++)
{
len=strlen(&names[i][0]);
for(k=len;k>0;k--)
{
if(names[i][k]==32)
{
a=k;
break;
}
}
if(j==0)
{
c=j;
for(k=0,f=0;names[i][k]!=0;k++)
{
if(names[i][k]==32||f==1)
{
names[i][j+1]=names[i][k];
f=1;
j++;
}
}
names[i][j+1]='\0';
j=c;
}
else if(j==a)
break;
else if(names[i][j]==32&&names[i][j+2]!=32)
{
c=j;
for(k=j+2,j=j+2,f=0;names[i][k]!=0;k++)
{
if(names[i][k]==32||f==1)
{
names[i][j]=names[i][k];
f=1;
j++;
}
}
names[i][j]='\0';
j=c;
}
}
}
for(i=0;i<5;i++)
printf("\n %s ",&names[i][0]);
getch();
return 0; // return int value
}
/*
source: http://bbb-letuscsolutions.blogspot.com/2009/10/chap9fe-creating-dynamic-calendar.html
*/
F (k) Write a program to count the number of occurrences of any two vowels in succession in a line of text. For example, in the sentence “Pleases read this application and give me gratuity” such occurrences are ea, ea, ui.
Show Solutions
Hide Solutions
Chapter: Puppetting On Strings
/* F (k) Write a program to count the number of occurrences of any two vowels in succession in a line of text. For example, in the sentence
“Pleases read this application and give me gratuity” such occurrences are ea, ea, ui.
*/
#include <stdio.h> //header file
#include <conio.h> //header file
#include <dos.h> //header file
int main() //main function
{
char sent[]="Write a program that will read the line and delete from it all occurrences of the word the ";
int i,c;
for(i=0,c=0;sent[i]!='\0';i++)
{
if((sent[i]==97||sent[i]==101||sent[i]==105||sent[i]==117)&&(sent[i+1]==97||sent[i+1]==101||sent[i+1]==105||sent[i+1]==111||sent[i+1]==117))
c++;
}
printf("\n There are %d occurences of two vowels in succession: ",c);
getch();
return 0; // return int value
}
/*
source: http://bbb-letuscsolutions.blogspot.com/2009/10/chap9fe-creating-dynamic-calendar.html
*/
Comments
Post a Comment