I have a task that is that the user enters a string and the deside if it is aligned r

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • destinius
    New Member
    • May 2014
    • 1

    I have a task that is that the user enters a string and the deside if it is aligned r

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    
    #define MAX 200
    
    void just_linea(char *linea, int ancho){
        int i, espacios;
        espacios = (ancho - strlen(linea))/2;
        for(i=0; i < espacios; i++)
           printf(" ");
        printf("%s", linea);
    }
    void just_cadena(char *cadena, int ancho){
        char subcadena[MAX];
        int i, total;
        total = (int)ceil((float)strlen(cadena) / ancho);
        for(i=0; i < total; i++){
            substring(cadena, subcadena, i*ancho, ancho);
            izquierda_linea(subcadena, ancho);
        }
    }
    
    
    void substring(char *cadena, char *subcadena, int inicio, int longitud){
        int i;
        for(i=0; i < longitud && inicio+i < strlen(cadena); i++)
           subcadena[ i ] = cadena[ inicio+i ];
        subcadena[ i ] = '\0';
    }
    void centrar_linea(char *linea, int ancho){
        int i, espacios;
        espacios = (ancho - strlen(linea)) / 2;
        for(i=0; i < espacios; i++)
           printf(" ");
        printf("%s", linea);
    }
    void derecha_linea(char *linea, int ancho){
        int i, espacios;
        espacios = ancho - strlen(linea);
        for(i=0; i < espacios; i++)
           printf(" ");
        printf("%s", linea);
    }
    void izquierda_linea(char *linea, int ancho){
    /*    int i, espacios;
        espacios = ancho + strlen(linea);
        for(i=0; i < espacios; i++)
           printf(" ");*/
        printf("%s", linea);
    }
    void izquierda_cadena(char *cadena, int ancho){
        char subcadena[MAX];
        int i, total;
        total = (int)ceil((float)strlen(cadena) / ancho);
        for(i=0; i < total; i++){
           substring(cadena, subcadena, i*ancho, ancho);
           izquierda_linea(subcadena, ancho);
        }
    }
    
    void centrar_cadena(char *cadena, int ancho){
        char subcadena[MAX];
        int i, total;
        total = (int)ceil((float)strlen(cadena) / ancho);
        for(i=0; i < total; i++){
           substring(cadena, subcadena, i*ancho, ancho);
           centrar_linea(subcadena, ancho);
        }
    }
    void derecha_cadena(char *cadena, int ancho){
        char subcadena[MAX];
        int i, total;
        total = (int)ceil((float)strlen(cadena) / ancho);
        for(i=0; i < total; i++){
           substring(cadena, subcadena, i*ancho, ancho);
           derecha_linea(subcadena, ancho);
        }
    }
    int main(){
        char cadena[MAX];
        int wut;
        printf("Ingrese cadena: ");
        gets(cadena);
        printf("(1)-Alinear a la izquierda\n(2)-Alinear a la derecha\n(3)-centrar\n(4)-justificar\n\n");
        scanf("%d",&wut);
        switch(wut){
            case (1):
                printf("\n\nCadena alineada a la izquierda:\n");
                fflush(stdin);
                izquierda_cadena(cadena, 80);
                printf("\n");
                system("PAUSE");
            break;
            case (2):
                printf("\n\nCadena alineada a la derecha:\n");
                fflush(stdin);
                derecha_cadena(cadena, 80);
                printf("\n");
                system("PAUSE");
            break;
            case (3):
                printf("\n\nCadena centrada:\n");
                centrar_cadena(cadena, 80);
                printf("\n");
                system("PAUSE");
            break;
            case (4):
                printf("\n\nCadena justificada:\n");
                just_cadena(cadena,80);
                printf("\n");
                system("PAUSE");
            break;
        }
    Last edited by Rabbit; May 9 '14, 04:03 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • techboy
    New Member
    • Apr 2014
    • 28

    #2
    Please be clear with Your question and doubt

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      I would think that the string (cadena) is right justified if the rightmost char is not whitespace. So if the length of the string (ancho) is 10,then if cadena[9] is not whitespace, the string is right justified. Remember, cadena[9] is the 10th char. strlen will report 10.

      Conversely, if cadena[0] is not whitespace, then the string is left justified.

      Depending upon the data contained in the string, the string may be both right justified and left justified at the same time.

      Comment

      Working...