Help with inverse...

This is a discussion on Help with inverse... within the C Programming forums, part of the General Programming Boards category; I want to enter 10 words in a vector and print the inverse. #include <stdio.h> #include <conio.h> #include <string.h> void ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    23

    Help with inverse...

    I want to enter 10 words in a vector and print the inverse.

    #include <stdio.h>
    #include <conio.h>
    #include <string.h>

    void reverse(char str[]);

    Code:
     
    main()
    {
    clrscr();
    char str[200],str1[200];
    int len;
    int i;
    int j;
    
    for(int i=1; i<=10; i++){
    printf("\nEnter your nickname:");
    gets( str[i]);
    len=strlen(str);
    for ( i=0,j=len-1;i<len;i++,j--)
      str1[i]=str[j];
    str1[i]='\0';
    puts(str1);
    getch(); }
    }

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Do you have a specific question, or were you just looking to be chastised by the forum pedantics on your coding and posting practices?
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And what part of this are you struggling with?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Use strrev() and print as many reverse numbers as you want :d
    Also don't you think it would be good if you indent your code properly?Here you are only writing only 10 lines so it's not a big deal but do you think you would be able to debug your code if you write more than 1000 lines of code?which braces are for which that itself you wouldn't be able to distinguish

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Boy are these cboard types ever tough! You are not far from being close to getting what you want though.

    Normally I think you should refer to an array as an array and not a vector, but anyway, lets take a look at your code, indented and with the necessary includes:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
    	clrscr();     // what's this and why is it here?
    	char str[200],str1[200];
    	int len;
    	int i;
    	int j;
    		
    	for(int i=1; i<=10; i++){  // you already declared i as an int
    		printf("\nEnter your nickname:");
    		gets( str[i]);		//gets requires a string buffer; str[X] is a single character
    		len=strlen(str);
    		for ( i=0,j=len-1;i<len;i++,j--)
    		  str1[i]=str[j];
    		str1[i]='\0';
    		puts(str1);
    		getch(); }	// not a standard C command and unneccessary anyway
    }
    Now something that can be compiled and might bring you closer to your goal:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
    	char str[200],str1[200];
    	int len, i, j;
    
    	for (i=1; i<=10; i++){
    		printf("\nEnter your nickname:");
    		gets(str);
    		len=strlen(str);
    		for ( i=0,j=len-1;i<len;i++,j--)
    			str1[i]=str[j];
    		str1[i]='\0';
    		puts(str1);
    	}
    }
    This just does the same thing ten times. It doesn't accumulated anything in str; for that you probably need another string buffer:

    Code:
     #include <stdio.h>
    #include <string.h>
    
    int main()
    {
    	char buffer[20],str[200],str1[200];
    	int len, i, j;
    
    	for (i=1; i<=10; i++){
    		printf("\nEnter your nickname:");
    		gets(buffer);
    		if (i>1) strcat(str,buffer);
    		else strcpy(str,buffer);
    		len=strlen(str);
    		for ( i=0,j=len-1;i<len;i++,j--)
    			str1[i]=str[j];
    		str1[i]='\0';
    		puts(str1);
    	}
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    You could have at least said something about NOT using gets(), and using fgets() instead.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    If I see someone using gets() with like a 4k buffer or something, I usually do not utter a peep. But with a 20 byte buffer I do have to ask what the writer is expecting to happen. You do realize that upset users are prone to typing stuff like

    Code:
    asdfkl;wjerq0981341=3-4908a;fkljadfjk;l1234l;jkadf;jkl
    At least that is what I do when a command line program keeps complaining about every little thing that its crazy input demands. So keep in mind that you shouldn't really ever use gets() but if you feel you must, don't use it with such rediculously small spans of memory.

  8. #8
    Hacker MeTh0Dz's Avatar
    Join Date
    Oct 2008
    Posts
    111
    Ehh whatever I was bored, saw this topic, had my editor opened, and so I coded an implementation. I don't think there are any bugs, so this should help you out.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int revstr(char * orig, char ** new_rev);
    
    int main(int argc, char * argv[])
    {
    	char * org = "Google would more than likely reveal the answer, but I was bored...";
    	char * new_s = malloc(200 * sizeof(char));
    	if (!new_s) return 0;
    	
    	revstr(org, &new_s);
    	
    	printf("&#37;s\n", new_s);
    	
    	free(new_s);
    	return 0;
    }
    
    int revstr(char * orig, char ** new_rev)
    {
    	if (orig == NULL || *new_rev == NULL) return -1;
    	int i = 0;
    	int j = 0;
    	
    	while (orig[i] != '\0') i++;
    	
    	while (--i >= 0) {
    		(*new_rev)[j] = orig[i];
    		j++;
    	}
    	(*new_rev)[j] = '\0';
    	
    	return 0;
    }

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    How about something even more simplistic?

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    int revstr(char * orig, char ** new_rev);
    
    int main(int argc, char * argv[])
    {
    	char * org = "Google would more than likely reveal the answer, but I was bored...";
    	char * new_s = 0;
    
    	revstr(org, &new_s);
    	
    	printf("&#37;s\n", new_s);
    	
    	free(new_s);
    	return 0;
    }
    
    int revstr(char * orig, char ** new_rev)
    {
    	char *tmp;
    
    	if(!*new_rev)
    	{
    		if(!(tmp = malloc(strlen(orig) + 1)))
    			return 1;
    	} else
    		if(!(tmp = realloc(*new_rev, strlen(orig) + 1)))
    			return 1;
    	strcpy(orig, tmp);
    	strrev(tmp);
    
    	*new_rev = tmp;
    	return 0;
    }
    Yours is probably faster, but mine copes with stuff a little nicer... unless you don't initialize the pointer you pass to the function.

  10. #10
    Hacker MeTh0Dz's Avatar
    Join Date
    Oct 2008
    Posts
    111
    Nothing wrong with multiple solutions.

    I hadn't even thought about doing it that way actually.

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    There certainly is nothing wrong with a second solution. I have the double threat motivating me....bordem and spanish homework.

    I think my way of accepting the pointer parameter combined with your actually copying over the string in reverse would probably be the most optimal solution (particularly if you copy in int-sized chunks).
    Last edited by master5001; 10-29-2008 at 08:53 PM.

  12. #12
    Hacker MeTh0Dz's Avatar
    Join Date
    Oct 2008
    Posts
    111
    Spanish... lol...... Got my third and final year of that done last year, hopefully I want have to take that crap in College.

    Also, if I get bored enough I may time the implementations and post the results. I'd much rather do that then coding something useful, lol.

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    >>hopefully I want have to take that crap in College

    I am not so fortunate.

  14. #14
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I speak spanish damn near fluently and had four years of it back in high school.

  15. #15
    Hacker MeTh0Dz's Avatar
    Join Date
    Oct 2008
    Posts
    111
    I guess speaking it fluently would be pretty cool. Although I just don't have the desire to learn a foreign language.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need to Calculate the Inverse of A Matrix.
    By jordan_230 in forum C Programming
    Replies: 6
    Last Post: 11-03-2008, 06:14 PM
  2. Multiplicative inverse question
    By Overworked_PhD in forum Tech Board
    Replies: 3
    Last Post: 04-02-2008, 10:58 AM
  3. How to inverse a trig?
    By Diablo02 in forum C# Programming
    Replies: 4
    Last Post: 11-07-2007, 10:11 PM
  4. ROW OPERATIONS (finding Inverse) (im going crazy)
    By alexpos in forum C Programming
    Replies: 1
    Last Post: 11-20-2005, 10:07 AM
  5. Inverse Tan
    By spidereen in forum C++ Programming
    Replies: 2
    Last Post: 03-04-2003, 08:55 AM

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