473,473 Members | 1,563 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to save a string with white spaces on an array

13 New Member
I want to save and print a full name in one array. The program would look like this:

Enter your full name: Mary Smith

Your full name is Mary Smith.

but I don't know how. what i did was i made two arrays. the first array was for the first name and the second array was for the last name. So the program would look like this already..

Enter your first name: Mary
Enter your last name: Smith

Your full name is: Mary Smith

but the problem would occur when writing the first name, for example:

Enter your first name: Mary Grace
Enter your last name: Your full name is Mary Grace.

The second and third line would print suddenly. And this is not how i want my program to be..

My source code of my program looks like this:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. int main()
  3. {
  4.    unsigned a[10],b[10];
  5.  
  6.    printf("Enter your first name: ");
  7.    scanf("%s",a);
  8.  
  9.    printf("Enter your last name: ");
  10.    scanf("%s",b);
  11.  
  12.    printf("%s",a);
  13.    printf(" ");
  14.    printf("%s",b);
  15.  
  16.    return 0;
  17. }
  18.  
please help me...
Sep 17 '07 #1
3 2568
kreagan
153 New Member
I want to save and print a full name in one array. The program would look like this:

Enter your full name: Mary Smith

Your full name is Mary Smith.

but I don't know how. what i did was i made two arrays. the first array was for the first name and the second array was for the last name. So the program would look like this already..

Enter your first name: Mary
Enter your last name: Smith

Your full name is: Mary Smith

but the problem would occur when writing the first name, for example:

Enter your first name: Mary Grace
Enter your last name: Your full name is Mary Grace.

The second and third line would print suddenly. And this is not how i want my program to be..

My source code of my program looks like this:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. int main()
  3. {
  4.    unsigned a[10],b[10];
  5.  
  6.    printf("Enter your first name: ");
  7.    scanf("%s",a);
  8. }
  9.  
please help me...
I didn't get the error you described. However, scanf("%s",a); did produce an error since a is not a string but an unsigned. (unsigned what? I don't know). When I changed that, the program ran how I expected it to run.

So I'm kind of confused what problem you are having.
Sep 17 '07 #2
kreagan
153 New Member

Enter your first name: Mary
Enter your last name: Smith

Your full name is: Mary Smith

but the problem would occur when writing the first name, for example:

Enter your first name: Mary Grace
Enter your last name: Your full name is Mary Grace.

The second and third line would print suddenly. And this is not how i want my program to be..
I duplicated your problem. You entered "Mary Grace". With scanf, the delimitor is a space. Therefore a is assigned Mary. When the second scanf is commanded, b then is assigned Grace without waiting for another user's input.

If you don't want to assume that the user's names are 1 word, use a while loop and break when your input is a return character.
Sep 17 '07 #3
buging
1 New Member
here's the solution

just instead of 'scanf ()' function use the 'gets()' to get the full name with sapces
and use the 'puts()' function instead of printf();

format for gets()

gets(arr name);

ex: gets(name)

puts(arr name);

ex: puts(name);
Sep 20 '07 #4

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

Similar topics

6
by: BadOmen | last post by:
I have a text file that I want to save from my program. But I don't want to save the empty lines. I want to delete everything after the last character, Is that possible? Then when I read the...
5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
6
by: Mark | last post by:
Hi, My page has a table with many columns such that the right-side of the table gets chopped off when printed. I specify a table width of 100%, but otherwise no cell dimensions are specified. ...
9
by: Durgesh Sharma | last post by:
Hi All, Pleas help me .I am a starter as far as C Language is concerned . How can i Right Trim all the white spaces of a very long (2000 chars) Charecter string ( from the Right Side ) ? or how...
4
by: dchan1 | last post by:
String line = " I am new to c# "; How to use Regex to split the string so that It would return a String array with: token = "I" token = "am" token = "new" token =...
16
by: Charles Law | last post by:
I have a string similar to the following: " MyString 40 "Hello world" all " It contains white space that may be spaces or tabs, or a combination, and I want to produce an array...
8
by: david.lindsay.green | last post by:
Hello all, I am quite new a web scripting and making web pages in general and I have stumbled across a problem I have as yet been unable to solve. I am trying to take the contents of a textarea box...
1
by: Brian Mitchell | last post by:
I'm sorry if this is the wrong group but I couldn't find one relating to cryptography. I have a byte array that I am encrypting using the System.Cryptography classes and it encrypts just...
3
by: stathisgotsis | last post by:
Hello everyone, Trusting K&R2 i thought until recently that spaces are ignored in scanf's format string. Reading arguments to the contrary confused me a little. So i now ask: Is...
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...
0
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,...
0
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...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
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...
0
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 ...

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.