Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
//
//INCLUDE files for :str_manipulation
//**************************************
//
#include
#include
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
Terms of Agreement:
By using this code, you agree to the following terms...
1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for langauges that allow it) freely and with no charge.
2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
3) You may link to this code from another website, but ONLY if it is not wrapped in a frame.
4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
//
#include
#include
void check_chars(char *);
void check_vowels(char *);
void upper_case(char *);
void lower_case(char *);
void no_words(char *);
void palindrome(char *);
void target_word_occur(char *,char *);
void reverse_word(char *);
void main(void)
{
char sentence[81],word[81];
puts("Enter a sentence or word: ");
gets(sentence);
puts("Enter a target word: ");
gets(word);
check_chars(sentence);
check_vowels(sentence);
upper_case(sentence);
lower_case(sentence);
palindrome(sentence);
no_words(sentence);
target_word_occur(sentence,word);
reverse_word(word);
return;
}
//to find no. of words in sentence
void no_words(char *line)
{
int len = 0,no = 0,i;
len = strlen(line);
for(i=0;i < len;i++){
if(line[i] == '\0')
printf("No words were printed\n");
else
{
if(line[i] == ' ')
no++;
}
}
printf("No of words is %d\n",no+1);
}/*end of no_words*/
//converts sentence to lowercase
void lower_case(char *line)
{
int len = 0,i;
len = strlen(line);
for(i=0;i < len;i++){
if(line[i] >= 'A' && line[i] <= 'Z')
line[i] = line[i] + 32;
}
printf("lower case - %s\n\n",line);
}/*end of lower_case function*/
//converts sentence to uppercase
void upper_case(char *line)
{
int len = 0,i;
len = strlen(line);
for(i=0;i < len;i++){
if(line[i] >= 'a' && line[i] <= 'z')
line[i] = line[i] - 32;
}
printf("upper case - %s\n\n",line);
}/*end of upper_case function*/
//counts the no. of each vowel in senten
// ce
void check_vowels(char *line)
{
int len = 0,no = 0,noa = 0,noe = 0,noi = 0,noo = 0,nou = 0,i;
len = strlen(line);
for(i=0;i < len;i++){
if(line[i] == 'a' || line[i] == 'e' || line[i] == 'i' || line[i] == 'o' || line[i] == 'u')
no++;
}
printf("\nNo of vowels is %d\n",no);
for(i=0;i < len;i++){
if(line[i] == 'a')
noa++;
if(line[i] == 'e')
noe++;
if(line[i] == 'i')
noi++;
if(line[i] == 'o')
noo++;
if(line[i] == 'u')
nou++;
}
printf("No of 'a' is %d\n",noa);
printf("No of 'e' is %d\n",noe);
printf("No of 'i' is %d\n",noi);
printf("No of 'o' is %d\n",noo);
printf("No of 'u' is %d\n",nou);
}/*end of check_vowels function*/
//counts the no. of characters in senten
// ce
void check_chars(char *line)
{
int len = 0,no = 0,i;
len = strlen(line);
for(i=0;i < len;i++){
if(line[i] == ' ')
no++;
}
printf("\nNo of spaces is %d\n",no);
printf("\nNo of chars is: %d\n",len);
printf("\nNo of letters is: %d\n",len-no);
}/*end of check_chars function*/
//checks if a word is a palindrome,i.e.
// it is spelt the same
//if you turn the word backwards eg. mad
// am
void palindrome(char *line)
{
int len = 0,i;
len = strlen(line);
//lower_case(line);
for(i = 0;i < len;i++){
if(line[i] != line[len-i-1]){
printf("sentence/word entered is NOT a palindrome - %s\n\n",line);
break;
}
else{
printf("sentence/word entered is a palindrome - %s\n\n",line);
break;
}
}
}/*end of palindrome function*/
//counts the no. of times a target word
// appears in sentence
void target_word_occur(char *sentence,char *word)
{
char *tokenPtr;
int count = 0,i = 0;
tokenPtr = strtok(sentence," ");
while(tokenPtr != NULL){
if(strcmp(word,tokenPtr)==0)
count++;
i++;
tokenPtr = strtok(NULL," ");
}
printf("The number of times \"(%s)\" appears is %d\n",word,count);
//printf("The number of words in string is %d\n",i);
}/*end of target word occur*/
//function reverses a word
void reverse_word(char *word)
{
int i=0,j=0,len=0;
char temp[50];
len = strlen(word);
//strcpy(word2,word);
for(i=len-1,j=0;i >= 0;i--,j++)
temp[j] = word[i];
temp[j] = '\0';
printf("The target word reversed is %s\n\n",temp);
}/*end of reverse_word*/
Other 5 submission(s) by this author