+ All Categories
Home > Documents > Programs

Programs

Date post: 11-Nov-2014
Category:
Upload: nikita-malik
View: 17 times
Download: 4 times
Share this document with a friend
Description:
Programs in C
Popular Tags:
13
Code for Program to find total vowels in the string in C Programming #include <stdio.h> #include <conio.h> void main(){ int chasc,i,cnt=0; char ch,str[40]="" ; clrscr(); printf("*****Vowels Demo*****\n\n" ); printf("enter string : " ); scanf("%[^\n]s" ,str); for (i=0;str[i]!='\0' ;i++) { chasc = str[i]; switch (chasc){ case'a' : case'A' : case'e' : case'E' : case'i' : case'I' : case'o' : case'O' : case'u' : case'U' : cnt++; } } printf("\nTotal number of vowels is %d" ,cnt); getch(); } Code for Program to count word from user entered text or sentence in C Programming #include <stdio.h> #include <conio.h> void main() { char a[100],b[10],t[20]; int i=0,n=0,w=1; clrscr(); printf( "\n Enter The String : " ); scanf( "%[^\n]" ,a); printf( "\n Enter The Word Which Count : " );
Transcript
Page 1: Programs

Code for Program to find total vowels in the string in C Programming

#include <stdio.h>#include <conio.h>void main(){ int chasc,i,cnt=0; char ch,str[40]=""; clrscr(); printf("*****Vowels Demo*****\n\n"); printf("enter string : "); scanf("%[^\n]s",str); for(i=0;str[i]!='\0';i++) { chasc = str[i]; switch(chasc){ case'a' : case'A' : case'e' : case'E' : case'i' : case'I' : case'o' : case'O' : case'u' : case'U' : cnt++; } }

printf("\nTotal number of vowels is %d",cnt);getch();}

Code for Program to count word from user entered text or sentence in C Programming

#include <stdio.h>#include <conio.h> void main() { char a[100],b[10],t[20]; int i=0,n=0,w=1; clrscr(); printf("\n Enter The String : "); scanf("%[^\n]",a); printf("\n Enter The Word Which Count : "); scanf("%s",b); while(a[i]!='\0') { t[n]=a[i]; if(a[i]==' ') { t[n]='\0'; //printf("\n %s",t); n=-1; if(strcmp(t,b)==0) w++;

Page 2: Programs

} i++; n++; } printf("\n %s comes %d times ",b,w-1); getch(); }

/* ****** OUTPUT ******

Enter The String : MICHEL IS GOOD IN C C IS GOOD LANGUAGE

Enter The Word Which Count : GOOD

GOOD comes 2 times*/

Write a program vowels.cpp that reads in text one character at a time and counts the number of vowels ’a’, ’e’, ’i’, ’o’ and ’u’ in the text. Both lower case and upper case vowels should be counted together. When a period appears in the input text, the program prints the number of each vowel and halts.

also must use a while loop

this is what i have but i cannot figure out the rest any help??

#include<iostream>using namespace std;

int main(){int va(0); //number of a'sint ve(0); //number of e'sint vi(0); //number of i'sint vo(0); //number of o'sint vu(0); //number of u's

char c; //text that is entered

cout<<"Enter text: ";cin>> c;

while (i=0;i<=c; i++){if (i==a || i==A){cout<<" Number of a's: "<<va++<<endl;}if (i==e || i==E){cout<<" Number of e's: "<<ve++<<endl;}if (i==i || i==I)

Page 3: Programs

{cout<<" Number of i's: "<<vi++<<endl;}if (i==o || i==O){cout<<" Number of o's: "<<vo++<<endl;}if (i==u || i==U){cout<<" Number of u's: "<<vu++<<endl;}}

return 0;} 

//C PROGRAM TO CONCATINATE TWO LINKED LISTS

#include<stdio.h># include<conio.h># include <malloc.h>struct node{int data;struct node *link;};void main(){int size1,size2,i, num;struct node *ptr,*ptr2,*result,*temp;struct node * concat(struct node *,struct node *);void display(struct node *);void add(struct node **,int );clrscr();ptr=NULL;ptr2 =NULL;result=NULL;printf("enter the size of 1st list \n");scanf("%d",&size1);printf("enter the elements\n");for(i=1;i<=size1;i++){scanf("%d",&num);add(&ptr,num);}printf("enter the size of 2st list \n");scanf("%d",&size2);printf("enter the elements\n");for(i=1;i<=size2;i++){scanf("%d", &num);add(&ptr2, num);}result = concat(ptr,ptr2);printf("the elements in concat list\n");display(result);getch();}// add an node to the listvoid add(struct node **q,int num){struct node *temp;temp = *q;

Page 4: Programs

if(*q==NULL){*q=malloc(sizeof(struct node));temp = *q;}else{while((temp->link)!=NULL){temp=temp->link;}temp->link = malloc(sizeof(struct node));temp=temp->link;}temp->data = num;temp->link = NULL;}// display the elements in the listvoid display(struct node *pt){while(pt!=NULL){printf(" %d\n",pt->data);pt=pt->link;}}// concatenation of listsstruct node *concat(struct node *p, struct node *q){struct node *x,*r;if(p==NULL)r=q;if(q==NULL)r=p;else{x=p;r=x;while(x->link!=NULL)x=x->link;x->link=q;}return(r);} 

//C PROGRAM TO CONCATINATE TWO LINKED LISTS

#include<stdio.h># include<conio.h># include <malloc.h>struct node{int data;struct node *link;};void main(){int size1,size2,i, num;struct node *ptr,*ptr2,*result,*temp;struct node * concat(struct node *,struct node *);void display(struct node *);

Page 5: Programs

void add(struct node **,int );clrscr();ptr=NULL;ptr2 =NULL;result=NULL;printf("enter the size of 1st list \n");scanf("%d",&size1);printf("enter the elements\n");for(i=1;i<=size1;i++){scanf("%d",&num);add(&ptr,num);}printf("enter the size of 2st list \n");scanf("%d",&size2);printf("enter the elements\n");for(i=1;i<=size2;i++){scanf("%d", &num);add(&ptr2, num);}result = concat(ptr,ptr2);printf("the elements in concat list\n");display(result);getch();}// add an node to the listvoid add(struct node **q,int num){struct node *temp;temp = *q;if(*q==NULL){*q=malloc(sizeof(struct node));temp = *q;}else{while((temp->link)!=NULL){temp=temp->link;}temp->link = malloc(sizeof(struct node));temp=temp->link;}temp->data = num;temp->link = NULL;}// display the elements in the listvoid display(struct node *pt){while(pt!=NULL){printf(" %d\n",pt->data);pt=pt->link;}}// concatenation of listsstruct node *concat(struct node *p, struct node *q){struct node *x,*r;if(p==NULL)r=q;if(q==NULL)

Page 6: Programs

r=p;else{x=p;r=x;while(x->link!=NULL)x=x->link;x->link=q;}return(r);} 

//C PROGRAM TO IMPLEMENT REVERSE LINKED LIST

#include<stdio.h>#include<stdlib.h>#include<string.h>struct node{int no;struct node *next;};struct node *head=NULL,*temp,*LLt;void addrecord();void main(){int ch;clrscr();while (1){printf("\n add element (number) TO LIST\n 0 TO EXIT / END");scanf("%d", &ch)if (ch==0){exit(0);}else{AddElement(ch,*LLt );}printf("the reverse of linked list is %d " reverse(*LLt));} void AddElement(ch,*LLt ){struct node *temp;temp=(struct node*)malloc(sizeof(struct node));temp->no=ch; if (LLt==NULL){LLt=temp;temp->next=NULL; }else{ LLt->next=temp;temp->next=NULL; }}node *reverse(node *first){node *temp = NULL; if(first->next != NULL){

Page 7: Programs

temp = reverse(first->next);temp->next = first;return first;}elsereturn first;}

http://books.google.co.in/books?id=ZgGzBt074e8C&pg=PA257&lpg=PA257&dq=program+that+takes+a+list+pointed+by+LIST+and+traverses+it+in+such+a+manner+that+after+travel+the+links+of+the+visited+nodes+become+reversed&source=bl&ots=FHlfB4bhDn&sig=PLQihVymWxUDeiGRfW1QufhL_Gg&hl=en&sa=X&ei=ByzEUOSnFtGGrAfLw4DgBQ&ved=0CDUQ6AEwAg#v=onepage&q=program%20that%20takes%20a%20list%20pointed%20by%20LIST%20and%20traverses%20it%20in%20such%20a%20manner%20that%20after%20travel%20the%20links%20of%20the%20visited%20nodes%20become%20reversed&f=false

Write a C program to reverse digits of a numberMay 30, 2009

ITERATIVE WAYAlgorithm:

Input: num(1) Initialize rev_num = 0(2) Loop while num > 0 (a) Multiply rev_num by 10 and add remainder of num divide by 10 to rev_num rev_num = rev_num*10 + num%10; (b) Divide num by 10(3) Return rev_num

Example:num = 4562rev_num = 0

rev_num = rev_num *10 + num%10 = 2num = num/10 = 456

Page 8: Programs

rev_num = rev_num *10 + num%10 = 20 + 6 = 26num = num/10 = 45

rev_num = rev_num *10 + num%10 = 260 + 5 = 265num = num/10 = 4

rev_num = rev_num *10 + num%10 = 265 + 4 = 2654num = num/10 = 0

Program:

#include <stdio.h>

 /* Iterative function to reverse digits of num*/int reversDigits(int num){    int rev_num = 0;    while(num > 0)    {        rev_num = rev_num*10 + num%10;        num = num/10;    }    return rev_num;}

 /*Driver program to test reversDigits*/int main(){    int num = 4562;    printf("Reverse of no. is %d", reversDigits(num));

     getchar();    return 0;}

Time Complexity: O(Log(n)) where n is the input number.

RECURSIVE WAYThanks to Raj for adding this to the original post.

#include <stdio.h>;

 /* Recursive function to reverse digits of num*/int reversDigits(int num){

Page 9: Programs

  static int rev_num = 0;  static int base_pos = 1;  if(num > 0)  {    reversDigits(num/10);    rev_num  += (num%10)*base_pos;    base_pos *= 10;  }  return rev_num;}

 /*Driver program to test reversDigits*/int main(){    int num = 4562;    printf("Reverse of no. is %d", reversDigits(num));

     getchar();    return 0;}

Print reverse of a string using recursionJune 19, 2009

Write a recursive C function to print reverse of a given string.

Program:

# include <stdio.h>

 /* Function to print reverse of the passed string */void reverse(char *str){   if(*str)   {       reverse(str+1);       printf("%c", *str);   }}

 /* Driver program to test above function */int main(){   char a[] = "Geeks for Geeks";   reverse(a);   getchar();   return 0;}

Page 10: Programs

Explanation: Recursive function (reverse) takes string pointer (str) as input and calls itself with next location to passed pointer (str+1). Recursion continues this way, when pointer reaches ‘\0′, all functions accumulated in stack print char at passed location (str) and return one by one.

C programming code/* String reverse in c*/

#include<stdio.h>#include<string.h> main(){ char arr[100];  printf("Enter a string to reverse\n"); gets(arr);  strrev(arr);  printf("Reverse of entered string is \n%s\n",arr);  return 0;}

/* Second method */

C program to reverse a string using pointers: Now we will invert string using pointers or without using library function strrev.

#include<stdio.h> int string_length(char*);void reverse(char*); main() { char string[100];  printf("Enter a string\n"); gets(string);  reverse(string);  printf("Reverse of entered string is \"%s\".\n", string);  return 0;} void reverse(char *string) { int length, c; char *begin, *end, temp;  length = string_length(string); 

Page 11: Programs

begin = string; end = string;  for ( c = 0 ; c < ( length - 1 ) ; c++ ) end++;  for ( c = 0 ; c < length/2 ; c++ ) { temp = *end; *end = *begin; *begin = temp;  begin++; end--; }} int string_length(char *pointer){ int c = 0;  while( *(pointer+c) != '\0' ) c++;  return c;}

C program to reverse a string using recursion#include<stdio.h>#include<string.h> void reverse(char*,int,int); main(){ char a[100];  gets(a);  reverse(a, 0, strlen(a)-1);  printf("%s\n",a);  return 0;} void reverse(char *x, int beg, int end){ char a, b, c;  if ( beg >= end ) return;   c = *(x+beg); *(x+beg) = *(x+end); *(x+end) = c;  reverse(x, ++beg, --end);}

Reverse string program executable.

Page 12: Programs

Output of program:


Recommended