find .c in a string

This is a discussion on find .c in a string within the C Programming forums, part of the General Programming Boards category; Hello, I am creating a program named pointc2pointo wich will take a list of arguments and find the ones that ...

  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
    22,302
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    134
    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
    22,302
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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 05:14 AM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,302
    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 05:25 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,302
    Refer to my edit.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,302
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 07:51 PM
  2. find a string
    By fazla in forum C Programming
    Replies: 6
    Last Post: 05-28-2010, 12:18 PM
  3. set<string> find
    By lawrenced in forum C++ Programming
    Replies: 3
    Last Post: 07-21-2009, 06:00 PM
  4. std::string::find vs std::find
    By petry in forum C++ Programming
    Replies: 17
    Last Post: 07-08-2009, 06: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21