Thread: Help with the question.(array of pointers to strings)

  1. #1
    F#ck me Freddy!!
    Join Date
    Sep 2013
    Location
    jaipur
    Posts
    79

    Help with the question.(array of pointers to strings)

    the question is :

    Write a program that uses an array of pointers to strings str[ ]. Receive two strings str1 and str2 and check if str1 is embedded in any of the strings in str[ ]. If str1 is found, then replace it with str2.
    Code:
    char *str[ ] = {
                        "We will teach you how to...",
                        "Move a mountain",
                        "Level a building",
                        "Erase the past",
                        "Make a million",
                        "...all through C!"
                    } ;
    For example if str1 contains "mountain" and str2 contains "car", then the second string in str should get changed to "Move a car".

    Now i have made a program to compare and remove the whole string in str but how to compare particular word here i have no idea.just need the idea how to do that

    my code :

    Code:
    /*30/10/13 22:15
    
    */
    #include<stdio.h>
    
    
    main( )
    {
     int i,a,x;
     char *str[ ] = {
                        "We will teach you how to...",
                        "Move a mountain",
                        "Level a building",
                        "Erase the past",
                        "Make a million",
                        "...all through C!"
                    } ;
     char str1[30],str2[30];
     printf("Enter str1 :");
     scanf("\n%[^\n]s",str1);
     printf("Enter str2 :");
     scanf("\n%[^\n]s",str2);
     for(i=0;i<6;i++)
     {
      a=strcmp(str[i],str1);
      if(a==0)
      {
         str[i]=str2;
      }
     }
     for(x=0;x<6;x++)
     printf("\n%s",str[x]);
    }

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Look into strstr for finding a substring in a string.
    But I'm not sure how you're going to replace a substring in a string literal!?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by coder1
    how to compare particular word here i have no idea
    Read up on strstr.

    By the way, note that you cannot actually change the contents of str[i] because it is a string literal. Rather, you should make a copy of the string, and then change that. In fact, you should declare str to be an array of pointers to const char.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array , Strings and Pointers
    By freak-guy in forum C Programming
    Replies: 2
    Last Post: 08-02-2011, 07:49 AM
  2. array of pointers of strings?
    By thefalcon79 in forum C Programming
    Replies: 6
    Last Post: 01-13-2011, 02:26 AM
  3. Array of pointers to strings (more than 1 way)
    By towed in forum C Programming
    Replies: 4
    Last Post: 11-17-2010, 10:44 AM
  4. Replies: 2
    Last Post: 04-27-2008, 03:39 AM
  5. Concatenating strings (dynamic array using pointers)
    By Tankndozer in forum C Programming
    Replies: 8
    Last Post: 07-01-2004, 07:27 AM

Tags for this Thread