-2

Why does my coding stop after print "Enter a for aligned or i for indented"?

I've already tried separating the code, like the way the coding would be read/processed, and asked my friends that are also in this class what I'm doing wrong....please help

#include <stdio.h>
#include<stdlib.h>
#define MaxSize 1024
void split(char array[], int start, int length);
void splitAndMove(char array[], int start, int length);
int main(){
    int n,i;
    char ch;
    char string[MaxSize];
    printf("Enter the number of character:\n");
    scanf("%d", &n);
    printf("Enter the character array:\n");
    fflush(stdin);
    for(i = 0;i < n;i++){
        scanf("%c", &string[i]);
        printf("Enter a for aligned or i for indented:\n");
        fflush(stdin);
        scanf("%c", &ch);
        if(ch == 'a')
           split(string, 0, n);
        else if(ch == 'i')
           splitAndMove(string, 0, n);
        return 0;
    }
}
void split(char array[], int start, int length){
    if (start >= length){
        return;
    }
    printf("*%c*\n", array[start]);
    split(array, start+1, length);
}
void splitAndMove(char array[], int start, int length){
    int i;
    if(start >= length){
        return;
    }
    for(i = 0; i <= start;i++){
         printf(" ");
    }
    printf("*%c*\n", array[start]);
    splitAndMove(array, start+1, length);
}

I should expect the result to look like:

*H*
 *E*
  *L*
   *P*

when i is input and:

*H*
*E*
*L*
*P*

when a is input

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Deb_kiM
  • 1
  • 1

2 Answers2

0

Double check your for loop block after asking for the character array.

Also change scanf("%c", ...) to scanf(" %c, ...) since "%c" does not remove trailing whitespace.

0

Just done some modification in code instead of scanf("%c",&ch); used scanf(" %c",&ch);

Removed fflush(stdin); and done some changes in length and also removed for loop as there was no need of loop as u can take string input with %s in scanf

#include <stdio.h>
#include<stdlib.h>
#define MaxSize 1024
void split(char array[], int start, int length);
void splitAndMove(char array[], int start, int length);
int main(){
    int n,i;
    char ch;
    char string[MaxSize];
    printf("Enter the number of character:\n");
    scanf("%d", &n);
    printf("Enter the character array:\n");
    scanf("%s", string);
    printf("Enter a for aligned or i for indented:\n");
    scanf(" %c", &ch);
    if(ch == 'a')
        split(string, 0, n);
    else if(ch == 'i')
        splitAndMove(string, 0, n);
    return 0;
}
void split(char array[], int start, int length){
    if (start >= length){
        return;
    }
    printf("*%c*\n", array[start]);
    split(array, start+1, length);
}
void splitAndMove(char array[], int start, int length){
    int i;
    if(start >= length){
        return;
    }
    for(i = 0; i <= start;i++){
        printf(" ");
    }
    printf("*%c*\n", array[start]);
    splitAndMove(array, start+1, length);
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459