Thread: C programing need help.

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    9

    C programing need help.

    Task:
    Write a function that adds the word in a sentence. The function has the following prototype:
    void add (char * sentence, char * word);
    Punctuation should be added immediately after the word. The function is necessary to dynamically
    allocate memory needed to add a word.
    In the main program, declare a pointer sentences and Dynamic Allocate enough memory
    to enter 10 characters. Enter the word and call function add. Adding repeat until
    load point.
    Write a function that will remove the word from the sentences that are not anagrams, in a way that other signs
    moves to the left. The function has the following prototype:
    void remove (char * sentence);
    In the main program call the function and display the newly created sentence.
    Write a function that will change the order of words in a sentence (first and last word of changing place,
    second and penultimate place for change, etc). The function has the following prototype:
    void replace (char * sentence);
    In the main program call the function and display the newly created sentence.

    I managed to get to here:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    void add(char *, char *);
    void remove(char *);
    void replace(char *);
    
    int main(){
        int i;
        char word[20];
        char *sentence;
        sentence=(char *) calloc(10,sizeof(char));
        do{
            printf("Enter word, for stop enter '.' :\n");
            scanf("%s", word);
            add(sentence,word);
        }while(strchr(sentence,'.')==0);
        printf("%s",sentence);
        remove(sentance);
        printf("%s",sentence);
        replace(sentance);
        printf("%s",sentence);
    
    return 0;
    }
    void add(char *sentence, char *word){
        char space[2]=" ";
        sentence=realloc(sentence,(strlen(sentence)+strlen(word)+1));
        if(*sentence==0)
            strcat(sentence,word);
        else if(ispunct(word[0])==0){
            strcat(sentence,space);
            strcat(sentence,word);
        }
        else
            strcat(sentence,word);
    }
    void remove(char *sentence){
    
    }
    void replace(char *sentence){
    
    }
    program: codeblocks
    compiler: GNU GCC
    I do not know how to solve functions remove and replace.
    Thanks in advance!

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You don't need to use calloc() there, malloc() is fine.

    For the remove:

    1) find the first letter of the word with strstr().
    2) replace it with '\0'.
    3) advance the pointer returned by strstr by the length of the word.
    4) strcat the original str and the pointer together.

    The reason for adding '\0' is to terminate the string there, but this does not replace the rest of the data in the array. So when you call strcat(), the data you're pointer points to will be tacked on at the '\0'. You can then realloc smaller by the length of the word.

    Replace is the same thing with a couple more steps. So try and get the remove to work first. If you have trouble, post it here.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Adding repeat until load point.
    What does this mean ^^^^^^^ ?

    And welcome to the forum, BadBlueBoy!

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    9
    Quote Originally Posted by Adak View Post
    What does this mean ^^^^^^^ ?

    And welcome to the forum, BadBlueBoy!
    for the end of the sentence enter '.'

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Also, you should limit the input length this way to prevent a buffer overflow on word:

    Code:
    scanf("%19s", word);  // max 19 characters
    Making word substantially larger -- say 256 bytes -- might be a good idea too. That is still a very trivial amount of memory, and it is on the stack anyway, which has a preset size usually in the MB's.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    9
    Quote Originally Posted by MK27 View Post
    1) find the first letter of the word with strstr().
    2) replace it with '\0'.
    3) advance the pointer returned by strstr by the length of the word.
    4) strcat the original str and the pointer together.

    The reason for adding '\0' is to terminate the string there, but this does not replace the rest of the data in the array. So when you call strcat(), the data you're pointer points to will be tacked on at the '\0'. You can then realloc smaller by the length of the word.

    Replace is the same thing with a couple more steps. So try and get the remove to work first. If you have trouble, post it here.
    how will this help me check whether the word is anagram?
    Last edited by badblueboy; 12-15-2011 at 10:03 AM.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by badblueboy View Post
    how will this help me check whether the word is anagram?
    It won't, I suppose you would have to do that first, but determining whether a word is an anagram or not should be fairly simple. Make that a separate function, and loop thru the sentence using something like this:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void) {
    	char str[] = "the quick brown fox jumped over the lazy red dog",
    		word[128], *p = str;
    
    	while (*p && sscanf(p, "%127s", word)) {
    		puts(word);
    		p += strlen(word);
    		while (*p == ' ') p++;
    	}
    	return 0;
    }
    In place of the puts(), you would be checking for an anagram and removing it if necessary. That will require a bit more logic WRT (not) advancing the pointer afterward.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    Dec 2011
    Posts
    9
    Perhaps I am wrong explain, but I must find anagreme contained in a sentence.
    example:
    imput:
    i love, vole and snake.
    output:
    love, vole.
    Last edited by badblueboy; 12-15-2011 at 10:43 AM.

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Sorry, I was thinking of palindromes, but I guess there aren't many individual words like that.

    I guess you would want to start by checking if the words are the same length. Then use strchr() on the second word for each letter from the first word. If you make it thru all the letters, the words are anagrams.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Registered User
    Join Date
    Dec 2011
    Posts
    9
    Quote Originally Posted by MK27 View Post
    Sorry, I was thinking of palindromes, but I guess there aren't many individual words like that.

    I guess you would want to start by checking if the words are the same length. Then use strchr() on the second word for each letter from the first word. If you make it thru all the letters, the words are anagrams.
    I know that is not much in English, but we in Croatia have much more anagrams. how to separate the words in the string, but punctuation marks remain on the place, and then to check for anagrams, and print only the anagrams and punctuation marks.

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Are there non-ascii characters in Croatian? If you have to work with that, it's going to be trickier.

    WRT to punctuation, you might want to write a function to get the length of the word sans trailing punctuation:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int wordlength (char *word) {
    	int i = 0;
    	while ( (word[i] >= 'a' && word[i] <= 'z')
    	  || (word[i] >= 'A' && word[i] <= 'Z') )
    		i++;
    	return i;
    }
    
    int main(void) {
    	char str[] = "the. quick; brown. Fox,",
    		word[128], *p = str;
    
    	while (*p && sscanf(p, "%127s", word)) {
    		printf("%s %d\n", word, wordlength(word));
    		p += strlen(word);
    		while (*p == ' ') p++;
    	}
    
    	return 0;
    }
    Output:
    the. 3
    quick; 5
    brown. 5
    Fox, 3



    Based on the specification in the OP, "Punctuation should be added immediately after the word", so this should work to give you the word length for the purpose of the anagram check.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #12
    Registered User
    Join Date
    Dec 2011
    Posts
    9
    Only the English alphabet



    I would really appreciate if you could write me that code.
    Last edited by badblueboy; 12-15-2011 at 11:50 AM.

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by badblueboy View Post
    I would really appreciate if you could write me that code.
    Nope.
    Announcements - General Programming Boards

    I've given you quite a bit of code as it is. Go back and see if you can write a function that will check if two words are anagrams, as per my suggestion in post #9. If you have trouble, post your attempt.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  14. #14
    Registered User
    Join Date
    Dec 2011
    Posts
    9
    Code:
     char *p = recenica, rij[11],*rj;
        int dulj[10]={0};
        while (*p && sscanf(p, "% 10s", rij)) {
            dulj[i]=duljina(rij);
            rj[i]=rij;   <-assignment makes integer from pointer without a cast
    why i get this error?

  15. #15
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    because rij (horrible variable name btw) is an array, which gets converted to a pointer in that context. rj[i] is a char, which is an "integer type". So it's trying to convert a char pointer to a char. Also, rj is not pointing to anything, so it will likely crash if you get it to compile.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C programing for DNA...
    By S16 in forum C Programming
    Replies: 9
    Last Post: 04-29-2009, 09:25 AM
  2. c programing help
    By hot love in forum C Programming
    Replies: 5
    Last Post: 03-20-2009, 03:30 PM
  3. C programing
    By flame82 in forum C Programming
    Replies: 2
    Last Post: 05-07-2008, 02:35 PM
  4. programing
    By NiVaG in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 10-03-2004, 09:19 AM
  5. C++ Programing
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 10-06-2001, 04:13 PM