Thread: Reversing String Problem

  1. #1
    C-no_Ob Bennie98's Avatar
    Join Date
    Oct 2010
    Location
    Ledeberg, Ghent, East-Flanders, Belgium
    Posts
    49

    Reversing String Problem

    Hi
    I'm learning to script in C and i've noticed it's not as simple as i want it to be...

    I am following the Emulov website to start learning c and in chapter 3 he tells me to make a program that has to have an input of your first and last name. each separately given in a separate string. Then he asks me to put the strings behind each other in the output and when it has done that it also has to put the strings in reverse behind each other.

    I've tried to make it work.. but when the inputnames are too long it cut's them off. and with the last name the program has no problem but with the first name it gives a bunch of crap in front of the name..

    it's really annoying me and when i look up the sollution on google.. the answers are all too advanced for me to understand..:/

    i'm just learning about arrays and multidimensional arrays.. don't know how to work with pointers or little parts of programming defined in the same program..


    my program is as follows(also an attachment to be sure):
    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main(void){
        char content, inhoud2;
        char naam[0], voor[0];
        int length=strlen(naam)-1, lengte=strlen(voor)-1;
        
        printf("Geef uw voornaam in: ");
        scanf("%s",voor);
        printf("Geef uw naam in: ");
        scanf("%s",naam);
        
        printf("U bent dus %s %s\n\n", voor, naam);
        
         printf("In omgekeerde volgorde is dat:");
        for(inhoud2=length;inhoud2>-1;inhoud2--){
         printf("%c", naam[inhoud2]);}
         printf("\n");
        for(content=lengte+1;content>-1;content--){
         printf("%c", voor[content]);} 
       
       
        
    getch();
    
    }

    i'm sorry about the dutch words but it shouldn't matter to the advanced scripters among us...

    already thx and hoping to hear something soon...

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    If your tutorial book/site has this line
    char naam[0], voor[0];
    It's time to leave it behind...
    I'd recommend The C programming Language, The C Book,...
    Btw, C is not a scripting language...

  3. #3
    C-no_Ob Bennie98's Avatar
    Join Date
    Oct 2010
    Location
    Ledeberg, Ghent, East-Flanders, Belgium
    Posts
    49
    i already have the book: the C programming language.
    but since it is written in english and somethings not explained well enough.. i sought out an other tool.


    char naam[0], voor[0]; is something i have written.. it doesn't give examples on how to make that program ... it tells you what the kinds of arrays and strings are and then you have to make your own program... (that's just for now...) it's only chapter three u see..

    and sorry.. didn't know there being a difference in scripting and programming...

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    I think you are using gcc compiler?
    Array of size 0 is strictly speaking, wrong and compiler should give you error.
    But gcc by default uses gcc C. not standard C.
    So always use gcc -std=c89 -Wall -pedantic-errors source.c to compile your source file.

  5. #5
    C-no_Ob Bennie98's Avatar
    Join Date
    Oct 2010
    Location
    Ledeberg, Ghent, East-Flanders, Belgium
    Posts
    49
    i'm not using gcc compiler. although not that i know of.
    i'm using dev-c++ as a typing aid so i'm using that compiler too.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Bennie98
    i'm not using gcc compiler. although not that i know of.
    i'm using dev-c++ as a typing aid so i'm using that compiler too.
    gcc is the C compiler that is packaged with Dev-C++.
    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

  7. #7
    C-no_Ob Bennie98's Avatar
    Join Date
    Oct 2010
    Location
    Ledeberg, Ghent, East-Flanders, Belgium
    Posts
    49
    yezz... owkay then... didn't know that one....

    so i'll try a different one then?

    edit: could you point me to a good + free editor/compiler for C - NOT C++ ??
    edit2: in FAQ<compilers there are a couple of them but also the one i'm using (which isn't suited for C-compiling..) that's why i'm asking for a better one...
    Last edited by Bennie98; 10-22-2010 at 07:27 AM.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Bennie98 View Post
    Code:
        char content, inhoud2;
        char naam[0], voor[0];
    You need to give these buffers a pre-set size... Perhaps 50 characters or so, to allow room for the longest name you're likely to encounter.

    Code:
    char naam[50];
    char voor[50];

  9. #9
    C-no_Ob Bennie98's Avatar
    Join Date
    Oct 2010
    Location
    Ledeberg, Ghent, East-Flanders, Belgium
    Posts
    49
    yez... i've done that... but i wanted it to be variable... so it doesn't matter how many characters a name has...

    i've already been able to run this program but it was always a little bit cut off
    I mean it's never enough... i don't know what kind of names they have in countries like india or afghanistan or something like that.... but i think some of them are very long...

    ps: i did what you said, but it didn't work...
    it still cuts off... :/

    the last name prints only 3 characters and the first name only 4 characters
    Last edited by Bennie98; 10-22-2010 at 07:36 AM.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Bennie98 View Post
    yez... i've done that... but i wanted it to be variable... so it doesn't matter how many characters a name has...
    C doesn't know how to do that.

    To make it variable, you first have to know the length of the name and allocate memory dynamically... To know the length you have to store the name someplace, putting you right back to square one.


    i've already been able to run this program but it was always a little bit cut off
    I mean it's never enough... i don't know what kind of names they have in countries like india or afghanistan or something like that.... but i think some of them are very long...

    ps: i did what you said, but it didn't work...
    it still cuts off... :/
    If 50 isn't enough make 100 or 250 character buffers... really, no harm no foul.

    You can accurately get the length of the name strings using strlen(), theres no reason to subtract 1... which is probably why you're getting clipping.

    The reason you're getting garbage printouts now is that you are going beyond the bounds of your 0 byte buffer and displaying garbage from unallocated memory, which just happens to be on the program's stack. I'm quite frankly surprised it's not crashing your system.
    Last edited by CommonTater; 10-22-2010 at 07:50 AM.

  11. #11
    C-no_Ob Bennie98's Avatar
    Join Date
    Oct 2010
    Location
    Ledeberg, Ghent, East-Flanders, Belgium
    Posts
    49
    omg you can screw your system up by trying something ? ^^ very safe programming then

    and no it hasn't done that.... but it spewed out a whole rant of figures and symbols...

    and i read somewhere that you have to subtract 1 because of the first memory allocation which is always 0 in an array...

    but i'll change the things you said and i hope it's going to work

  12. #12
    C-no_Ob Bennie98's Avatar
    Join Date
    Oct 2010
    Location
    Ledeberg, Ghent, East-Flanders, Belgium
    Posts
    49
    i tried it... i changed the buffer to 250 and i deleted the minus 1

    now it still clips but now it shows only 4 characters in last name and 2 characters in first name...

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Bennie98 View Post
    i tried it... i changed the buffer to 250 and i deleted the minus 1

    now it still clips but now it shows only 4 characters in last name and 2 characters in first name...
    Ok... now move the line where you're calculating length so that it's AFTER your inputs. Or, even better, use strlen() in your loops...

    Code:
         int inhound2, content;
    
         printf("In omgekeerde volgorde is dat:");
        for(inhoud2=strlen(naam);inhoud2>0;inhoud2--){
         printf("%c", naam[inhoud2 - 1]);}
         printf("\n");
        for(content=strlen(voor);content>0;content--){
         printf("%c", voor[content - 1]);}
    Last edited by CommonTater; 10-22-2010 at 08:22 AM.

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Bennie98 View Post
    omg you can screw your system up by trying something ? ^^ very safe programming then
    I've had to reformat and reinstall after some of my more disastrous experiments... so yes, this dog really can eat your homework.

    and no it hasn't done that.... but it spewed out a whole rant of figures and symbols...
    Because you were working in unallocated memory.

    and i read somewhere that you have to subtract 1 because of the first memory allocation which is always 0 in an array...
    I think you are confusing the number of elements with the number of the element. A C array of 10 items has items numbered 0 to 9 ...

    but i'll change the things you said and i hope it's going to work
    Can it get any worse?

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main(void){
        char content, inhoud2;
        char naam[0], voor[0];
        int length, i;
        
        printf("Geef uw voornaam in: ");
        scanf("%s",voor);
        printf("Geef uw naam in: ");
        scanf("%s",naam);
        
        length=strlen(naam); 
        lengte=strlen(voor); //moved -1's removed, replaced comma with semi-colon
        printf("U bent dus %s %s\n\n", voor, naam);
    
        
         printf("In omgekeerde volgorde is dat:");
    
        //what you're printing are char's, but the index counter i is an int
        for(i=length;i>-1;i--) {
           printf("%c", naam[i]);  //indent subordinate code lines
        } //closing braces go right below the first char that started them - 'f' in this case
         printf("\n");
        for(i=lengte;i>-1;i--) {
           printf("%c", voor[content]);
        } 
        getch();
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polymorphism and generic lists
    By Shibby3 in forum C# Programming
    Replies: 9
    Last Post: 07-26-2010, 05:27 AM
  2. string problem
    By INeedSleep in forum C++ Programming
    Replies: 24
    Last Post: 11-08-2007, 11:52 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM

Tags for this Thread