Jump to content

Swapping Information in Array

- - - - -

  • Please log in to reply
5 replies to this topic

#1
UnknownFear

UnknownFear

    Learning Programmer

  • Members
  • PipPipPip
  • 47 posts
I am learning C and wanted to test something out. I want to swap a location and letter specified from the user into an array that will overwrite the previous elements. Here is my code:

#include <stdio.h>

/* Swap location of array with char specified, than print out array. */

main()
{
    /* Initialize variables */
    char letters[5]={'a','b','c','d','e'};
    char loc, let, temp;

    /* Get location and letter */
    printf("Enter the location in the array you want to swap: ");
    scanf("%c", &loc);
    printf("Enter letter you want to swap with specified location: ")
    scanf"%c", &let);
    
    /* Swap speified location and letter into array */

That is all I have so far, I can't think of how you would do it. Any help with this?

Blog - Twitter - Facebook - Skype - Email


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Do you mean you just want to over-write the current value in the array?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
UnknownFear

UnknownFear

    Learning Programmer

  • Members
  • PipPipPip
  • 47 posts

WingedPanther said:

Do you mean you just want to over-write the current value in the array?

Exactly. I want to enter a location in the array that I want to switch values, than enter a letter to switch into that value. If that makes sense :P

For example, say I enter a location of 0, so I want to switch the first element, Than, for the letter, I want to put 'z', so I type z. It should than switch the letter in location 0, "a", to the letter I specified, in this case, is "z".

Blog - Twitter - Facebook - Skype - Email


#4
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
letters[userChoice] = userLetter;
This will change value at userChoice index with whatever user enters. It would be wise to check if index is out of range.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#5
UnknownFear

UnknownFear

    Learning Programmer

  • Members
  • PipPipPip
  • 47 posts

Flying Dutchman said:

letters[userChoice] = userLetter;
This will change value at userChoice index with whatever user enters. It would be wise to check if index is out of range.

I'm just starting arrays. How would I put that in my program?

Blog - Twitter - Facebook - Skype - Email


#6
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1

    /* Swap speified location and letter into array */

    letters[loc] = let;

After this comment you posted in your original code.

Here's a teaser for ya :)

    *(letters + loc) = let;

This is excatly the same as code above, I heard it's even faster. And if we wanna be mean, we could write:

    loc[letters] = let;


A conclusion is where you got tired of thinking.
#define class struct    // All is public.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users