Help! Storing strings into arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Keo932
    New Member
    • Oct 2006
    • 8

    Help! Storing strings into arrays

    Im having trouble with the last part of an assignment of storing a user defined string into a one dimensional array. If anyone can help it would be much appreiated. Here's what it requests:
    • Prompts the user to enter a string and store it into one-dimensional array.

      Replace the content of the string with the string reversed and save into another array.

      Print both the original string and reversed one in this function.


    Code:
    void rev_str()
    {
    	
    	string message;
    	cin.ignore();
    
    	cout << "Enter a string:" << endl;
    	
    	getline(cin, message);
    	cout << message;
    	
    	
    	
    	
    }
  • sircool
    New Member
    • Oct 2006
    • 12

    #2
    I wrote a programm that can help you i put the source if can't understand i will help you again

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #define SIZE 120
    
    main()
    {
    int count=0;
    char name[SIZE];
    char *reversed;
    reversed=(char *)malloc(strlen(name)+1);
    printf("please type a name that will be reversed\n");
    gets(name);
    
    for(int i=strlen(name)-1;i>-1;i--)
    reversed[count++]=name[i];
    reversed[strlen(name)]=0;
    puts(name);
    puts(reversed);
    return 0;
    }

    Comment

    • D_C
      Contributor
      • Jun 2006
      • 293

      #3
      Are you using the STL string class? If so, you can call
      Code:
      reverse(s.begin(), s.end());
      to reverse the string.

      Comment

      • Keo932
        New Member
        • Oct 2006
        • 8

        #4
        ah so it was a for loop.. i was having trouble deciding which loop to use. Sircool, thanks for taking the time to help. Everything works perfectly now

        Comment

        Working...