Thread: problem working with char array

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    16

    problem working with char array

    Hi all, i got a bit of a problem...


    Code:
    char name[30];
    char man[30];
    .
    .
    .
    //some value assigned to name
    .
    .
    .
    sprintf(man, "%s", &name);//where name is of the format ABECD.aid
    If i had the above code, and later in the code wanted to asign to man the same value without the extension. how would i go about doing that.

    i do not know the lengh of name, and the name is different each time. so is the extension.


    all help is greatly appreciated.

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    37
    hi,

    Rigth now i can think of 3 methods of doing it...

    1. Write a function, where in you send the pointer of name and man and do a char by char copy till you get the "."

    2. Using strtok() with "." as the second parameter..

    3. strcpy name and man.. and then char by char check if its "." and when you find it replace it with '\0'

    there could be other ways too.. but i can think of this three rite now.. !!!

    Happy coding..
    Last edited by Machoscorpion; 07-27-2005 at 07:06 AM.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Quote Originally Posted by bradleym83
    Code:
    char name[30];
    char man[30];
    .
    .
    .
    //some value assigned to name
    .
    .
    .
    sprintf(man, "%s", &name);//where name is of the format ABECD.aid
    lose the &name, it's sprintf(man, "%s", name);

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    If you're 100% positive that a '.' will exist then you can do:
    Code:
    *strchr(man, '.') = '\0';
    If you understand what you're doing, you're not learning anything.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    If you're not, you can do
    Code:
    char *p = strchr(man, '.');
    if(p) *p = 0;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  2. confused with a char array problem
    By f6ff in forum C++ Programming
    Replies: 15
    Last Post: 05-02-2006, 09:12 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM