You are on page 1of 18

1.

write C programs for the following problems


a) Accept a string and count the number of vowels and consonants in it.

#include <stdio.h>
#include <string.h>

int main(){

char name[100]; int i, vowel=0,conso=0;

printf("Enter the any name: ");


scanf("%s", &name);

for (i = 0; i < strlen(name); i++){

if (name[i]=='a' || name[i]=='e' ||name[i]=='i' || name[i]=='o'||


name[i]=='u'){vowel++;}
else{conso++;}
}

printf("Number of Vowels: %d\nNumber of Consonants: %d\n",vowel,conso);


return 0 ;
}
b) Display a given string by replacing each letter with the next one, except letters
z and Z
which should be replaced by letters a and A, respectively. Thus, the string “for
loop is
very powerful” should be displayed as gps mppq jt wfsz qpxfsgvm.

#include <stdio.h>
#include <string.h>
int main(){

char alpha[26] = "abcdefghijklmnopqrstuvwxyz", caps[26] =


"ABCDEFGHIJKLMNOPQRSTUVWXYZ" , a[1000];
int i,j;
printf("Enter your thoughts!:\n");
fgets(a, sizeof(a), stdin);

for (i = 0; i<strlen(a);i++){
for(j = 0; j<26; j++){
if(a[i] == 'z'){printf("a");break;}
else{
if (a[i] == alpha[j])
{printf("%c",alpha[j+1]);}
}
if(a[i] == 'Z'){printf("A");break;}
else{
if(a[i] == caps[j])
{printf("%c",caps[j+1]);} } }}
printf("\n");
return 0;
}

2. Define functions for the following operations on strings:


a) Determine the length of the initial portion of a given string containing only
letters.

#include<stdio.h>
#include<string.h>
#include<ctype.h>
int main()
{
char s[30];
int n,i,j,m,k=0;
printf("enter string:");
scanf("%s",s);
n=strlen(s);
for(i=0;i<n;i++)
{

if(s[i]==' ')
{
break;
}
else if(isalpha(s[i])==0)
{
break;
}
else
k++;
}
printf("length=%d",k);
return 0;
}

b) Count the number of words in a given string.

#include<stdio.h>
#include<string.h>
int main()
{
char s[30];
int c=0,i;
printf("enter string:");
scanf("%[^\n]s",s);
int n=strlen(s);
for(i=0;i<n;i++)
{
if(s[i]==' ')
c++;
}
printf("no. of words=%d",c+1);
return 0;
}

c) Change the case of characters in a given string.

#include<stdio.h>
#include<string.h>
#include<ctype.h>
int main()
{
char s[30];
int c=0,i;
printf("enter string:");
scanf("%[^\n]s",s);
int n=strlen(s);
for(i=0;i<n;i++)
{
if(s[i]!=' ' && isupper(s[i])==0)
{
s[i]=(char)(s[i]-32);
}
else if(s[i]!=' '&& islower(s[i])==0)
{
s[i]=(char)(s[i]+32);
}
else
continue;

}
printf("the string is %s",s);
return 0;
}
d) Test Whether a given string is a palindrome or not, i.e., whether it reads
the same in the forward and backward direction, as in “never odd or even”
and “Don’t nod”.

#include<stdio.h>
#include<string.h>
#include<ctype.h>
char * delspc(char *s)
{
int c=0,i;
for(i=0;s[i]!='\0';i++)
{
if (s[i]!=' ')
{s[c]=s[i];
c++;
}
}
s[c]='\0';
printf("deleted str=%s\n",s);
return s;
}
int main()
{
char s[30];
int c=0,i,f,l,n;
printf("enter string:");
scanf("%[^\n]s",s);
delspc(s);
n=strlen(s);
f=0;
l=n-1;
for(i=0;i<n;i++)
{
if(s[i]!='\0')
{
while(s[f]==s[l] && f<l)
{
f++;
l--;
}
}
}
printf("%d\n",f);
printf("%d\n",l);
if(f<l)
printf("string not a palindrome.");
else
printf("string is palindrome.");
return 0;
}

e) Convert a given two-digit number to word.

#include<stdio.h>
#include<string.h>
void printword(int n)
{
char
one[50][50]={"","one","two","three","four","five","six","seven","eight","nine
","ten",

"eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eight
een","nineteen"};

char
ten[50][50]={"","","twenty","thirty","fourty","fifty","sixty","seventy","eighty
","ninety"};

if(n>19)
printf("%s %s",ten[n/10],one[n%10]);
else
printf("%s",one[n]);

}
int main()
{
int n;
printf("enter two digit no.:");
scanf("%d",&n);
printword(n);
return 0;
}

3. Write C programs for the following problems.a) Accept a sentence and


print the words in reverse order, e.g., the string “c is a very easy
programming language” should be printed as language programming easy
very a is C.

#include<stdio.h>
#include<string.h>
#include<ctype.h>
void rev(char s[50])
{
int n=strlen(s);
for(int i=n-1;i>=0;i--)
{
if(s[i]==' ')
{
s[i]='\0';
printf("%s ",&(s[i])+1);
}
}
printf("%s",s);
}

int main()
{
char s[30];
printf("enter string :");
scanf("%[^\n]s",s);
rev(s);
return 0;
}

b) Replace each consonant in a string with the next one except letters z and Z,
which should be replaced by letters b and B, respectively. Thus, the string
“programming in c is fun” should be modified as Qsohsanniph ip D it gup

#include<stdio.h>
#include<string.h>
#include<ctype.h>
int isvowel(char ch)
{
if(ch == 'A' || ch == 'a' || ch=='E'|| ch=='e' || ch=='I' || ch=='i' || ch=='O' ||
ch=='o' || ch=='U'|| ch=='u')
return 1;
else
return 0;
}

int main()
{
int n,i;
char s[50];
printf("enter string:");
scanf("%[^\n]s",s);
n=strlen(s);
for(i=0;i<n;i++)
{
if(isvowel(s[i])==0)
{
if(s[i]=='Z')
{
s[i]='B';
}
else if(s[i]=='z')
{
s[i]='b';
}
else if(s[i]==' ')
{
s[i]=' ';
}
else
{
s[i]=(char)(s[i]+1);
}
}

}
printf("\nthe replaced string is %s",s);
return 0;
}

4. Define functions for the following operations on strings:


a) Replace all occurrences within a substring with another string.

#include<stdio.h>
#include<string.h>
#include<ctype.h>
char *replace_str(char *str, char *orig, char *rep)
{
static char buffer[4096];
char *p;
int i=0;

while(str[i]){
if (!(p=strstr(str+i,orig))) return str;
strncpy(buffer+strlen(buffer),str+i,(p-str)-i);
buffer[p-str] = '\0';
strcat(buffer,rep);
printf("STR:%s \n",buffer);
i=(p-str)+strlen(orig);
}

return buffer;
}

int main(void)
{
char str[100],str1[50],str2[50];
printf("Enter a one line string..\n");
gets(str);
printf("Enter the sub string to be replaced..\n");
gets(str1);
printf("Enter the replacing string....\n");
gets(str2);
puts(replace_str(str, str1, str2));

return 0;
}

b) Convert a given integer number (represented using long int) to word (use
Indian conventions: thousand, lakh and crore).

#include<stdio.h>
#include<string.h>
#include<ctype.h>
void printword(int n, char ch[50])
{
char
one[50][50]={"","one","two","three","four","five","six","seven","eight","nine
","ten",

"eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eight
een","nineteen"};

char
ten[50][50]={"","","twenty","thirty","fourty","fifty","sixty","seventy","eighty
","ninety"};

if(n>19)
printf("%s %s",ten[n/10],one[n%10]);
else
printf("%s",one[n]);
if(n>0)
printf("%s ",ch);
}

int main()
{
long n;
printf("enter number(less than 99 crore):");
scanf("%d",&n);

printword((n/10000000)," crore");
printword((n/100000)%100," lakh");
printword((n/1000)%100," thousand");
printword((n/100)%10," hundred");
printword((n%100)," ");
printf("rupees");
printf(" only.\n");
return 0;
}

c) Convert a given string representing of a number to an integer number that


can be represented in a long int (use international conventions: thousand,
million and billion).
#include<stdio.h>
void printword(int N, char ch[100])
{
char
one[50][40]={"","one","two","three","four","five","six","seven","eight","nine
","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen
","eighteen","nineteen"};
char ten[50][40]={"","
","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety"};
char hun[50][40]={"","one hundred and ","two hundred and","three
hundred and","four hundred and","five hundred and","six hundred
and","seven hundred and","eight hundred and","nine hundred and"};

if (N>99)
{
printf("%s %s %s",hun[N/100],ten[(N/10)%10],one[N%10]);
}
else if ( N>19)
{
printf("%s %s",ten[N/10],one[N%10]);
}
else
{
printf("%s",one[N]);
}

if(N>0) printf("%s",ch);
}

int main()
{
long n;
printf("Enter the number(less than 999 billion):");
scanf("%d",&n);
printf("rupees ");

printword((n/1000000000)%1000," billion ");//three billion


printword((n/1000000)%1000," million ");//one hundred and twenty three
million
printword((n/1000)%1000," thousand ");//two hundred and thirty
thousand
printword((n/100)%10," hundred and ");//two hundred and
printword((n%100),"");//two
printf(" only.\n");
}
.

You might also like