Thread: Question about strcat

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    19

    Question about strcat

    I have to write a program. Its basically a "random self book writer".

    It uses random number generation to write sentences. It has 4 arrays of strings which are the databank for the words the program picks to construct the sentence. I'm nearly done with it, but there is one requirement I'm having problems with.

    The program needs to concatenate each word to the previous word in a seperate array large enough to hold the entire sentence. The only function I know of that can do this is strcat. I'm having problems concatenating the words using strcat. I need some help. Here is what I made so far.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main( void )
    {
    
    	int word1, word2, word3, word4;
    
    	char *article[] = { "The", "A", "One", "Some", "Any" };
    	char *noun[] = { "boy", "girl", "dog", "town", "car" };
    	char *verb[] = { "drove", "jumped", "ran", "walked", "skipped" };
    	char *preposition[] = { "to.", "from.", "over.", "under.", "on." };
    	char sentence[50]= "";
    
    	srand( time(0) );
    
            word1=  rand() &#37; 5;
    	word2= rand() % 5;
    	word3= rand() % 5;
    	word4= rand() % 5;
    
    
    
    
    	printf("%s\n", strcat(sentence,article[word1], noun[word2] ));
    
    	return 0;
    Last edited by Countfog; 05-02-2008 at 10:59 PM.

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    strcat() doesn't work like that; you can't provide multiple strings to tack on to the end. All it can do is take one string and add another, single string to the end.

    You could call strcat() multiple times; or use sprintf(). sprintf() is more analogous to what you appear to be trying to do.

    Do be careful. Neither strcat() nor sprintf() will check whether you're writing too much data to your target and bad things will happen if you do so!

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Maybe something like
    printf("&#37;s %s\n", article[word1], noun[word2]);
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    19
    Quote Originally Posted by Elysia View Post
    Maybe something like
    printf("%s %s\n", article[word1], noun[word2]);
    Yea that works, but this is in the chapter on string and character functions. It doesn't really concatenate them, so my professor might not be satisfied. I will try sprintf()

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    size_t sentence_length = sizeof sentence;
    size_t article_length = sizeof article[word1];
    size_t noun_length = sizeof noun[word2];

    strncat( sentence, article[word1], sentence_length > article_length ? article_length : sentence_length - 1 );
    sentence_length -= article_length;
    strncat( sentence, noun[word2], sentence_length > noun_length ? noun_length : sentence_length - 1 );

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    19
    Quote Originally Posted by citizen View Post
    size_t sentence_length = sizeof sentence;
    size_t article_length = sizeof article[word1];
    size_t noun_length = sizeof noun[word2];

    strncat( sentence, article[word1], sentence_length > article_length ? article_length : sentence_length - 1 );
    sentence_length -= article_length;
    strncat( sentence, noun[word2], sentence_length > noun_length ? noun_length : sentence_length - 1 );
    Never mind, it works much better with sprintf(), but thanks. Concatenate means to join words together, and sprintf() does that as well.
    Last edited by Countfog; 05-04-2008 at 03:26 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An interesting question about strcat
    By meili100 in forum C++ Programming
    Replies: 3
    Last Post: 07-07-2009, 12:59 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM