473,382 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5.  
  6. #define MAX 200
  7.  
  8. void just_linea(char *linea, int ancho){
  9.     int i, espacios;
  10.     espacios = (ancho - strlen(linea))/2;
  11.     for(i=0; i < espacios; i++)
  12.        printf(" ");
  13.     printf("%s", linea);
  14. }
  15. void just_cadena(char *cadena, int ancho){
  16.     char subcadena[MAX];
  17.     int i, total;
  18.     total = (int)ceil((float)strlen(cadena) / ancho);
  19.     for(i=0; i < total; i++){
  20.         substring(cadena, subcadena, i*ancho, ancho);
  21.         izquierda_linea(subcadena, ancho);
  22.     }
  23. }
  24.  
  25.  
  26. void substring(char *cadena, char *subcadena, int inicio, int longitud){
  27.     int i;
  28.     for(i=0; i < longitud && inicio+i < strlen(cadena); i++)
  29.        subcadena[ i ] = cadena[ inicio+i ];
  30.     subcadena[ i ] = '\0';
  31. }
  32. void centrar_linea(char *linea, int ancho){
  33.     int i, espacios;
  34.     espacios = (ancho - strlen(linea)) / 2;
  35.     for(i=0; i < espacios; i++)
  36.        printf(" ");
  37.     printf("%s", linea);
  38. }
  39. void derecha_linea(char *linea, int ancho){
  40.     int i, espacios;
  41.     espacios = ancho - strlen(linea);
  42.     for(i=0; i < espacios; i++)
  43.        printf(" ");
  44.     printf("%s", linea);
  45. }
  46. void izquierda_linea(char *linea, int ancho){
  47. /*    int i, espacios;
  48.     espacios = ancho + strlen(linea);
  49.     for(i=0; i < espacios; i++)
  50.        printf(" ");*/
  51.     printf("%s", linea);
  52. }
  53. void izquierda_cadena(char *cadena, int ancho){
  54.     char subcadena[MAX];
  55.     int i, total;
  56.     total = (int)ceil((float)strlen(cadena) / ancho);
  57.     for(i=0; i < total; i++){
  58.        substring(cadena, subcadena, i*ancho, ancho);
  59.        izquierda_linea(subcadena, ancho);
  60.     }
  61. }
  62.  
  63. void centrar_cadena(char *cadena, int ancho){
  64.     char subcadena[MAX];
  65.     int i, total;
  66.     total = (int)ceil((float)strlen(cadena) / ancho);
  67.     for(i=0; i < total; i++){
  68.        substring(cadena, subcadena, i*ancho, ancho);
  69.        centrar_linea(subcadena, ancho);
  70.     }
  71. }
  72. void derecha_cadena(char *cadena, int ancho){
  73.     char subcadena[MAX];
  74.     int i, total;
  75.     total = (int)ceil((float)strlen(cadena) / ancho);
  76.     for(i=0; i < total; i++){
  77.        substring(cadena, subcadena, i*ancho, ancho);
  78.        derecha_linea(subcadena, ancho);
  79.     }
  80. }
  81. int main(){
  82.     char cadena[MAX];
  83.     int wut;
  84.     printf("Ingrese cadena: ");
  85.     gets(cadena);
  86.     printf("(1)-Alinear a la izquierda\n(2)-Alinear a la derecha\n(3)-centrar\n(4)-justificar\n\n");
  87.     scanf("%d",&wut);
  88.     switch(wut){
  89.         case (1):
  90.             printf("\n\nCadena alineada a la izquierda:\n");
  91.             fflush(stdin);
  92.             izquierda_cadena(cadena, 80);
  93.             printf("\n");
  94.             system("PAUSE");
  95.         break;
  96.         case (2):
  97.             printf("\n\nCadena alineada a la derecha:\n");
  98.             fflush(stdin);
  99.             derecha_cadena(cadena, 80);
  100.             printf("\n");
  101.             system("PAUSE");
  102.         break;
  103.         case (3):
  104.             printf("\n\nCadena centrada:\n");
  105.             centrar_cadena(cadena, 80);
  106.             printf("\n");
  107.             system("PAUSE");
  108.         break;
  109.         case (4):
  110.             printf("\n\nCadena justificada:\n");
  111.             just_cadena(cadena,80);
  112.             printf("\n");
  113.             system("PAUSE");
  114.         break;
  115.     }
May 9 '14 #1
2 1225
techboy
28
Please be clear with Your question and doubt
May 9 '14 #2
weaknessforcats
9,208 Expert Mod 8TB
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.
May 10 '14 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Adriaan van Heerden | last post by:
hi, I'm very new to Access and have been searching newsgroups for a few days but can't find exactly what I need. I want to be able to do the following: * user enters patient's hospital number *...
4
by: santa19992000 | last post by:
can I use scanf to get input (some times user enters input sometimes not, just hit keyboard)?. It displays to enter IP address, if user wants to change, then he enters, otherwise he hits keyboard,...
1
by: tcb | last post by:
I have a RangeValidator setup for two input fields. The validator Type is set to Integer. If the user enters an alpha numeric, the validator works, however, if the user enters a space, the validator...
3
by: Allerdyce.John | last post by:
Hi, Is there an easy way in PHP which send outs different content depends on User Agent String value? If yes, is there an example/documentation? Thank you.
0
by: marek zegarek | last post by:
Hello! I need to write some code, that will change user agernt string in SYSTEM. Not for browsers. I know, that there is registry entry in...
2
by: eddy556 | last post by:
Hey, Im having trouble making a textbox change colour when a user enters it. I have managed to textBox2.BackColor = Color.Pink; place that in the code which seems to work but I wish for it to...
0
by: =?Utf-8?B?TWlrZTEz?= | last post by:
Sorry for the cross post from dontnet.framework but I'm hoping this may see more activity here. I have written an application to access a web service. The application runs at multiple sites and...
0
by: =?Utf-8?B?TWlrZTEz?= | last post by:
Sorry for the cross post; I'm hoping this will get a response here... I have written an application to access a web service. The application runs at multiple sites and must cross a proxy server....
1
by: henryrhenryr | last post by:
Hi I am trying to write a function to decode / decipher the user agent string. I've got so far but I wondered if anyone knows any quick ways to do this or any useful resources to help me finish. ...
3
by: abueno | last post by:
Ok, I have an if statement, and the user enters a value outside of the range.... say: int sco; //Variable used for score. if(sco< 0 || sco > 100) { cout<<"Error, You have to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.