Created
September 18, 2012 14:49
-
-
Save swaroopsm/3743537 to your computer and use it in GitHub Desktop.
Program asked by Maventic Company
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (c) 2012 Swaroop SM <[email protected]> | |
// This program is free: you can redistribute it and/or modify | |
// it under the terms of the GNU General Public License as published by | |
// the Free Software Foundation, either version 3 of the License, or | |
// (at your option) any later version. | |
// This program is distributed in the hope that it will be useful, | |
// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
// GNU General Public License for more details. | |
// <http://www.gnu.org/licenses/>. | |
// The program that was given to code for CMRIT MCA and M.Tech Students by Maventic Company. | |
// Input: | |
// Read a sentence | |
// Output: | |
// If each word in the sentence consists of more that 3 characters, display the ASCII Code of the first and last character of that particular word. | |
// If each word of the sentence consists of less than 3 characters, sum the ASCII codes of the first and last characters, and find the total of all! | |
// Finally, check whether the total is Prime or not! | |
#include<stdio.h> | |
// #include<conio.h> //Under windows platform | |
void check_prime(int); | |
void main(){ | |
int i,s=0,k,j,c=0,c2=0,c3,e1,e2,sum=0,e3; | |
char swa[50]; | |
system("clear"); //Replace with clrscr(); if running program under windows | |
printf("\nEnter the sentence --> "); | |
gets(swa); | |
printf("The sentence is: %s\n",swa); | |
i=0; | |
for(j=0;swa[j]!='\0';j++){ | |
c++; | |
} | |
for(j=0;swa[j]!='\0';j++){ | |
c2++; | |
if(c2==c){ | |
swa[j+1]=' '; | |
} | |
} | |
do{ | |
if(swa[i]==' '){ | |
k=i; | |
c3=0; | |
for(j=s;j<k;j++){ | |
c3++; | |
} | |
if(c3>=3){ | |
for(j=s;j<k;j++){ | |
printf("\nStarting Character is: %c and ASCII code: %d",swa[s],swa[s]); | |
printf("\nStarting Character is %c and ASCII code: %d",swa[k-1],swa[k-1]); | |
printf("\n-----------------------------------------------------\n"); | |
break; | |
} | |
} | |
if(c3<=2 && c3>1){ | |
for(j=s;j<k;j++){ | |
e1=(int)swa[s]; | |
e2=(int)swa[k-1]; | |
sum=sum+e1+e2; | |
break; | |
} | |
} | |
if(c3==1){ | |
e3=(int)swa[s]; | |
sum=sum+e3; | |
} | |
s=k+1; | |
} | |
i++; | |
}while(swa[i]!='\0'); | |
check_prime(sum); | |
//getch(); //Under windows platform | |
} | |
void check_prime(int n){ | |
int j,k,flag=0; | |
printf("\n"); | |
printf("\nSum of ASCII codes is: %d\n",n); | |
for(j=2;j<=n;j++){ | |
if(n%j==0){ | |
flag++; | |
} | |
else{ | |
} | |
} | |
if(flag==1){ | |
printf("\n%d is a prime number!\n\n",n); | |
} | |
else{ | |
printf("\n%d is not a prime number!\n\n",n); | |
} | |
//getch(); //Under windows platform | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment