tackleberry
tackleberry

Reputation: 15

shifting array element one by one

I got a homework question. I'm so close to complete program. I'm having trouble about one thing. This is the question:

Write a C program that generates and displays a character array of size 10 consisting of random English lower-case letters. The program then asks the user how many times the array will be right-shifted and displays the right shifted array at each right-shifting step. A sample program execution output is given below. ( Hint: Use the ASCII codes of the English lower-case letters which are 97, 98, ... 122 for a, b, ..., z, respectively, to generate the character array).

This is my code:

#include <time.h>
#include <stdio.h>
#include <stdlib.h>

void print_string (char *string){
 int i;
 for (i=0 ; i < 10 ; i ++){
    printf("%c ", string[i]);
    if (i == 9)
     printf("\n");
  }

} 
void random_string(char *string, unsigned length)
{
  /* Seed number for rand() */
  srand((unsigned int) time(0) + getpid());

  /* ASCII characters 97 to 122 */
  int i;  
  for (i = 0; i < length; ++i)
    {
      string[i] = (rand() % 26)+ 97;
    }

  string[i] = '\0';  
}

void reverse_string(char* str, int left, int right) {
  char* p1 = str + left;
  char* p2 = str + right;
  while (p1 < p2) {
    char temp = *p1;
    *p1 = *p2;
    *p2 = temp;
    p1++;
    p2--;

  }
}

void rotate(char* str, int k, int n) {

  reverse_string(str, 0, n-1);
  reverse_string(str, 0, k-1);
  reverse_string(str, k, n-1);

}

int main(void)
{
  char s[11];
  int i,shiftNum;

  random_string(s, 11);
  printf("Randomly constructed array is :\n");

  print_string(s);

  printf("Enter how many times array will be shifted: ");
  scanf("%d",&shiftNum);

  rotate(s,shiftNum,11);
  print_string(s);

}

What's wrong with this code? When I execute it with 1, I couldn't get the first reverse correctly and I want to display all shifting steps.

Upvotes: 0

Views: 250

Answers (1)

autistic
autistic

Reputation: 15632

For a start, it is atrocious that your lecturer/professor is telling you to use 97..122. C does not require that ASCII be the character set on every system, so this code is entirely non-portable, yet if you look at the history as far as Unix is concerned C is supposed to be a portable programming language. If you want to write this in a portable way, you need to store the characters in an array and select from that array:

char lowercase[] = "abcdefghijklmnopqrstuvwxyz";
string[i] = lowercase[rand() % (sizeof lowercase - 1)];

Now that we've covered that pedantic detail, Cool Guy indicated in a comment that this line of code is erroneous: string[i] = '\0';. He's correct about that.


This should also performed within main, not within random_string: srand((unsigned int) time(0) + getpid());. The reason is that calling random_string multiple times in the same second would result in the same "random string", which is very uncool.


scanf("%d",&shiftNum); can't guarantee success (that the user will input numeric data), and so can't guarantee that shiftNum will contain a sane value. You need to check the return value. For example:

if (scanf("%d", &shiftNum) != 1) {
    puts("Invalid shift count!\n");
    exit(0);
}

You should probably also consider using an unsigned type for shiftNum (and this will cause the corresponding format spec %d to change to something else, such as %u for unsigned int).


One more important task before I finish this task: You need to modify rotate to handle an input of 0 correctly, since some users might want to rotate/shift 0 times (as an alternative to not rotating/shifting at all). I'm confident that this should be an easy task for you.

Upvotes: 1

Related Questions