Thread: find .c in a string

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    29

    find .c in a string

    Hello,

    I am creating a program named pointc2pointo wich will take a list of arguments and find the ones that terminates by .c and replace it by .o

    so far I have that :
    Code:
    #include <stdio.h>
    
    int main (int argc, char*argv[]){
        int i;
        for (i=1;i<argc;i++){
    how do i look for .c in a string ? and how do I replace it ?

    thank you

  2. #2
    Registered User
    Join Date
    Apr 2012
    Posts
    29
    Well maybe it will be easier to understand I I type the actual instructions...

    We want to make a program called pointc2pointo that for any string taken as an argument, if it terminats by ".c", will be showed up to the screen replacing ".c" by ".o"
    Exemple :
    ./pointc2pointo abc.c ploum abc.h etoile.c plic
    returns
    abc.o etoile.o

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Start by writing a program that prints its command line arguments. Compile and test, fixing it until it works.

    Modify the program to print only those command line arguments that end with ".c". Compile and test, fixing it until it works.

    Modify the program to change the ".c" ending part of each such command line argument to ".o" and print the result. Compile and test, fixing it until it works.

    You're done.
    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

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    136
    You can take help of functions like strtok() (strtok_r() if working with multi-threaded application), sprintf().
    S_ccess is waiting for u. Go Ahead, put u there.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by maven
    You can take help of functions like strtok() (strtok_r() if working with multi-threaded application)
    There should be no need to use strtok. Rather than tokenise based on ".", just examine the last two characters of each command line argument, if the string has at least 2 characters.
    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

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    29
    Well I did that, and I am sure it is not what you expected me to do knowing that it doesn't work...
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main (int argc, char*argv[]){
        int i;
        int lenth;
        char argx[100];
        int nbarg;
        for (i=1;i<argc;i++){
            strcpy(argx, argv[i]);
            lenth = strlen(argv[i]);
            if (argx[lenth]=="c"&& argx[(lenth-1)]== ".") {
                printf("%s ",argv[i]);
                }
            }
            printf("\n");
        return 0;
        }
    Code:
    questionB1.c: In function ‘main’:
    questionB1.c:12:18: warning: comparison between pointer and integer [enabled by default]
    questionB1.c:12:41: warning: comparison between pointer and integer [enabled by default]

    well maybe itwill sound really nooby but seeing that i tryed strcmp(argx[lenth],"c")==0 but it is even worse...

    I use strcpy because i didn't want to to argv[i][lenth] which would have been a 2 dimentions table...
    Last edited by dekl; 06-07-2012 at 04:14 AM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    argx[lenth] is wrong: an array with lenth elements has a largest index of lenth-1. You should be comparing with argx[lenth-1] and argx[lenth-2] instead. Furthermore, you are comparing with characters, so it should be 'c' and '.', not "c" and ".". Plus, consider what happens if a command line argument consists of just one character.

    Oh, you don't need the argx array as you can use argv directly, and "lenth" should really be "length".

    EDIT:
    Quote Originally Posted by dekl
    I use strcpy because i didn't want to to argv[i][lenth] which would have been a 2 dimentions table...
    If you want to avoid that, all you need is:
    Code:
    char *argx;
    Then in the loop:
    Code:
    argx = argv[i];
    Last edited by laserlight; 06-07-2012 at 04:25 AM.
    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

  8. #8
    Registered User
    Join Date
    May 2012
    Location
    India
    Posts
    12
    if (argx[lenth]=="c"&& argx[(lenth-1)]== "."
    Use single quote 'c' than double quotes "c" as it seems you are comparing argx[length] with the string "c" as double quote means a string I guess. I hope it'll work.

  9. #9
    Registered User
    Join Date
    Apr 2012
    Posts
    29
    oh right, sorry for the spelling as you can tell it is not my native language...

    how can I use only argv[i] ? wont it make a two dimention table if I say argv[i][length] ?

    thanks again for the help!

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Refer to my edit.
    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

  11. #11
    Registered User
    Join Date
    Apr 2012
    Posts
    29
    true that it works like that but then what is strcpy for ?


    here is the final working program!

    Code:
    #include <stdio.h>
    #include <string.h>
     
    int main (int argc, char*argv[]){
        int i,j;
        int length;
        char *argx;
        for (i=1;i<argc;i++){
            argx = argv[i];
            length = strlen(argv[i]);
            if (argx[length-1]=='c'&& argx[(length-2)]== '.') {
            for (j=0;j<(length-1);j++) {
                printf("%c",argx[j]);}
                printf("o ");
                }
            }
            printf("\n");
        return 0;
        }

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by dekl
    what is strcpy for ?
    When you really need to copy an entire string to an array that has enough space to hold it.

    Quote Originally Posted by dekl
    here is the final working program!
    Good, but your indentation is slightly misleading. I might indent it as:
    Code:
    #include <stdio.h>
    #include <string.h>
      
    int main(int argc, char *argv[]) {
        int i, j;
        int length;
        char *argx;
        for (i = 1; i < argc; i++) {
            argx = argv[i];
            length = strlen(argv[i]);
            if (argx[length-1] == 'c' && argx[(length - 2)] == '.') {
                for (j = 0; j < (length-1); j++) {
                    printf("%c", argx[j]);
                }
                printf("o ");
            }
        }
        printf("\n");
        return 0;
    }
    Now, you should check that the length >= 2 before you start comparing characters. Then, you don't need that inner loop; you just need to change the last character to a 'o' once you know the string ends with ".c". After that, you can print the string with just a single printf.
    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

  13. #13
    Registered User
    Join Date
    Apr 2012
    Posts
    29
    Now, you should check that the length >= 2 before you start comparing characters. Then, you don't need that inner loop; you just need to change the last character to a 'o' once you know the string ends with ".c". After that, you can print the string with just a single printf.

    true that it is more logical this way...

    Code:
    #include <stdio.h>
    #include <string.h>
     
    int main (int argc, char*argv[]){
        int i,j;
        int length;
        char *argx;
        for (i=1;i<argc;i++){
            argx = argv[i];
            length = strlen(argv[i]);
        if (length>2){
                if (argx[length-1]=='c'&& argx[(length-2)]== '.') {
                argx[length-1]='o';
                printf("%s ",argx);
                    }
            }    
        }
        printf("\n");
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Find index of last char in search string in string?
    By Programmer_P in forum C++ Programming
    Replies: 6
    Last Post: 06-07-2010, 06:51 PM
  2. find a string
    By fazla in forum C Programming
    Replies: 6
    Last Post: 05-28-2010, 11:18 AM
  3. set<string> find
    By lawrenced in forum C++ Programming
    Replies: 3
    Last Post: 07-21-2009, 05:00 PM
  4. std::string::find vs std::find
    By petry in forum C++ Programming
    Replies: 17
    Last Post: 07-08-2009, 05:26 PM
  5. find a string based on the location of another string
    By rlilley in forum C Programming
    Replies: 3
    Last Post: 02-19-2009, 12:29 PM