Create an Interactive Program that accepts a string into an array. Count the number of times a character occurs in a given string then reverses the string and displays it. Implement everything (from input to output) using pointers.
here's my code:
CODE
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void sort(int a, char *p)
{
char *temp;
for(int i=0;i<a-1;i++)
{
for(int j=i+1;j<a;j++)
{
if(*(p+i)>*(p+j))
{
*temp=*(p+i);
*(p+i)=*(p+j);
*(p+j)=*temp;
}
}
}
}
void main()
{
clrscr();
char arr[100];
char arr1[30];
int arr2[30];
int count=1,b=0,q=0;
char *p,*p1;
int *p2;
p1=&arr1[q];
p2=&arr2[q];
int a;
cout<<"Enter a string: ";
gets(arr);
p=&arr[q];
a=strlen(arr);
cout<<"\n\n\n"<<strrev(arr);
sort(a,p);
cout<<"\n\n\n";
for(int i=0;i<strlen(arr);)
{
while(*(p+i)==*(p+i+1))
{
count++;
i++;
}
*(p1+b)=*(p+i);
*(p2+b)=count;
count=1;
b++;
i++;
}
for(i=0;i<b;i++)
{
cout<<"Character "<<*(p1+i)<<" occurs "<<*(p2+i)<<" time/s";
cout<<"\n\n";
}
getch();
}
well, there's nothing wrong with that code. I would just like to enhance it to be able to solve this problem:
Write a program to count the vowels and letters in free text given as standard input. Read text a character at a time until you encounter end-of-data.
Then print out the number of occurrences of each of the vowels a, e, i, o, u in th the text, the total number of letters, and each of the vowels as an integer percentage of the letter total.Suggested output format is:
Numbers of characters:
a 3; e 2; i 0; o 1; u 0; rest 17
Percentages of total:
a 13%; e 8%; i 0%; o 4%; u 0%; rest 73%
how can i solve this using the code i've made for the first problem?? please help me.. thanks a lot!
This post has been edited by bengt23648: 5 Mar, 2008 - 06:06 PM