How to delete a character from each word in the sentence
Apr 30, 2018 at 9:01am UTC
i was trying to do a program that ask you how many characters you wish to delete from each word
as example:
you enter the sentence : HI HOW ARE YOU
delimiter(strtok): space
number of characters to delete from each word: 1
the sentence becomes : I OW RE OU
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
#include <iostream.h>
#include <cstring>
char *mytok(char *,char *,int );
main(){
char s1[40];
char s2[40];
int n;
cout<<"Write down your sentence" <<endl;
cin.getline(s1,40,'\n' );
cout<<"Enter your token or delimiter, -i assure you can enter more than one-" <<endl;
cin.getline(s2,40,'\n' );
cout<<"How many characters you wish to delete from each word ? " <<endl;
cin>>n;
mytok(s1,s2,n);
}
char *mytok(char * s1,char * s2,int x){
int c;
char *sentence = new char [50];
sentence = strtok(s1,s2);
char *re = new char [40];
while (sentence != NULL){
c=0;
while (c<=x){
c++;
sentence++;
}
while (*sentence != '\0' ){
*re = *sentence;
sentence++;
re++;
}
sentence++;
}
cout<<re<<endl;
}
Apr 30, 2018 at 1:56pm UTC
That would be a piece of cake if done by means of std::string and std::stringstream.
Have you chosen to do it by C-style arrays for practice on them?
May 1, 2018 at 2:33am UTC
in our class we just use character arrays .
Topic archived. No new replies allowed.