473,513 Members | 3,777 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Printing string arrays

Tango Charlie
3 New Member
Anyone who can help,

I"m starting to pull my hair out about this...I had to use scanf to read in the strings. I have been working on this lil project for 3 days and can't figure out how to print the strings after they have been swapped. Mine goes a lil something like this...

printf("\nThese are the names that you entered: \n\n");
for (i = 0; i < N_STRINGS; ++i)
printf("%s\n", s[i]);
for (i = 0; i < N_STRINGS; ++i)
for (k = i + 1; k < N_STRINGS; ++k)
if (strcmp(s[i], s[k]) == 1){
swap(s[i], s[k]);
}
printf("\nThe names that you entered are arranged alphabeticaly: \n\n");
for (i = 0; i < N_STRINGS; ++i)
printf("%s\n", s[i]);
printf("\n");
return 0;
}

void swap(char *p, char *q)
{
char tmp = *p;

*p = *q;
*q = tmp;
}

The first char of each string are the only things alphabetized; I need the whole string swapped. Please help!!
Mar 26 '07 #1
2 2705
svlsr2000
181 Recognized Expert New Member
Anyone who can help,

I"m starting to pull my hair out about this...I had to use scanf to read in the strings. I have been working on this lil project for 3 days and can't figure out how to print the strings after they have been swapped. Mine goes a lil something like this...

printf("\nThese are the names that you entered: \n\n");
for (i = 0; i < N_STRINGS; ++i)
printf("%s\n", s[i]);
for (i = 0; i < N_STRINGS; ++i)
for (k = i + 1; k < N_STRINGS; ++k)
if (strcmp(s[i], s[k]) == 1){
swap(s[i], s[k]);
}
printf("\nThe names that you entered are arranged alphabeticaly: \n\n");
for (i = 0; i < N_STRINGS; ++i)
printf("%s\n", s[i]);
printf("\n");
return 0;
}

void swap(char *p, char *q)
{
char tmp = *p;

*p = *q;
*q = tmp;
}

The first char of each string are the only things alphabetized; I need the whole string swapped. Please help!!
In function swap try using strcpy();
char * temp = malloc (your Max size)
strcpy(temp,p);
strcpy(p,q)
strcpy(q,temp)
free temp;
Mar 26 '07 #2
Tango Charlie
3 New Member
In function swap try using strcpy();
char * temp = malloc (your Max size)
strcpy(temp,p);
strcpy(p,q)
strcpy(q,temp)
free temp;
Thanks a million! I couldn't get the swap() to work so I inserted the strcpy() in its place. The following worked well...

Expand|Select|Wrap|Line Numbers
  1. printf("\nThese are the names that you entered: \n\n");
  2.     for (i = 0; i < N_STRINGS; ++i)
  3.         printf("%s\n", s[i]);
  4.     for (i = 0; i < N_STRINGS; ++i)
  5.         for (k = i + 1; k < N_STRINGS; ++k)
  6.             if (strcmp(s[i], s[k]) == 1){
  7.                 strcpy_s(temp[k], s[i]);
  8.                 strcpy_s(s[i], s[k]);
  9.                 strcpy_s(s[k], temp[k]);
  10.             }
  11.     printf("\nThe names that you entered are arranged alphabeticaly: \n\n");
  12.     for (i = 0; i < N_STRINGS; ++i)
  13.         printf("%s\n", s[i]);
  14.         printf("\n");
  15.     return 0;
  16.  
Thanks again, svlsr!
Mar 26 '07 #3

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

Similar topics

4
4834
by: Jody Gelowitz | last post by:
I am having a problem with printing selected pages. Actually, the problem isn't with printing selected pages as it is more to do with having blank pages print for those pages that have not been selected. For example, if I were to have 5 pages with every second page printing, I would get the following results: Page 1 = Print OK Page 2 =...
2
16315
by: juliadream | last post by:
Can anyone tell my why print @array produces output different from print "@array" The second one embeds spaces between the items; the first one does not.
42
5857
by: junky_fellow | last post by:
Consider an implementation that doesn't use all bits 0 to represent a NULL pointer. Let the NULL pointer is represented by 0x12345678. On such an implementation, if the value of NULL pointer is printed will it be all 0's or 0x12345678 int main(void) { char *ptr; ptr = 0;
2
2216
by: Martin | last post by:
I have encountered a bug in print preview: In my program, data for printing is taken from arrays. When print preview is invoked or page is printed directly, it is ok, but when user prints from print preview, any data that was in array is not draw, so only thing printed is header and footer. Arrays with data are in the same scope as...
5
10072
by: Patrick De Ridder | last post by:
How can I turn what I want to print 90 degrees using the logic below? Please tell me the code with which to make the modification. Many thanks, Patrick. using System.ComponentModel; using System.Drawing; using System.Drawing.Printing; using System.IO;
4
3142
by: Rob T | last post by:
I have a small VB program that has a printing module...very simple....and works great. However, If I try to print to a generic printer, I get the following error: "The data area passed to a system call is too small". I found the following article, that I assume is similar to my problem, which is of little help:...
4
2752
by: Carl Tribble | last post by:
Is there a method in VB .NET to convert a numeric dollar amount to it's text equivalent for printing on a check? Thanks, -Carl
1
5686
by: hamil | last post by:
I am trying to print a graphic file (tif) and also use the PrintPreview control, the PageSetup control, and the Print dialog control. The code attached is a concatination of two examples taken out of a Microsoft book, "Visual Basic,Net Step by Step" in Chapter 18. All but the bottom two subroutines will open a text file, and then allow me...
3
3684
by: John Peterson | last post by:
Hello all! I'm at my wits end trying to search for what I assumed to be a relatively straightforward task. I have a Web application written in C#, and I have a button on the form that I want to print the current contents of the browser without bringing up the print dialog. At first I thought it was a simple matter to have the button's...
3
1587
momotaro
by: momotaro | last post by:
pre: - array pos containing the dots' position in the IP adress - array IP containing the IP string post:- return 4 arrays xx, yy, zz, tt containing the IP/4 digits problem(remaining from 1st version): - unexpected result while checking using printf example: - instead having "xx = 123" am having 123:) - third part of the IP 123 and some...
0
7270
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7178
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7397
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7563
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7125
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5102
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3252
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
1612
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
813
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.