1

How can I create a string array in the C language? Its length should be five, and a loop should be used to get input from the user to fill the array with. Finally, I should print all string values in the array to the user.

4
  • 7
    C or C++? These are separate languages... Also, what have you tried so far? This looks like homework, and while we'll help you out, we won't do the entire assignment for you. You learn nothing that way. Commented Apr 4, 2011 at 22:38
  • 6
    Homework? Read the C info and the C++ info. Commented Apr 4, 2011 at 22:41
  • It sounds like you mean an array of five strings. Remember that in C, a string is itself an array of characters; so you need an array of arrays (or pointers, but that's probably beyond what you've learned for now). Does that help? As the comments say, you probably won't find anyone here who will just write the code for you. However, if you are willing to make some effort and show the code you've written, people will help you find the errors in it. Commented Apr 4, 2011 at 23:44
  • I really hope you actually try to learn to code, otherwise you'll end up as one of the "programmers" that don't actually know how to code and give us all a bad reputation. Commented Jun 12, 2011 at 17:26

2 Answers 2

4

A string in C is really an array of characters. If you want to make a character array of length 256, you can do it like this:

char my_var[256];

You can read in a string of length 128 from standard input like this:

#include <stdio.h>
// ...
fgets(my_var,128,stdin);

You can print out a string like this:

printf("String is: %s",my_var);

A string is stored in the array character-by-character, and ends with the null character '\0'. Thus if my_var holds {'c','a','n','\0','s'}, then my_var looks like "can". But if the string does not end until it sees a null character. So if you fill a string character-by-character, you must append the '\0'. If you fill it with something like fgets, the null character is appended automatically. Also, note that '\0' is equal to zero.

Those are just easy howtos to get you started. As pmg said, we aren't here to do your homework for you, just give you tips. Look up fgets, printf, "strings in C", etc. If you get stuck, you can come back and ask a more specific question!

Sign up to request clarification or add additional context in comments.

1 Comment

Hmmm @bo perhaps mentioning the null terminator would be appropriate
0

try this for c,

string FileMeasure="Hello FILE!"
int TempNumOne=FileMeasure.size();
char Filename[100];
for (int a=0;a<=TempNumOne;a++)
    {
        Filename[a]=FileMeasure[a];
    }

and try this for java,

String[] words = {"ace", "boom", "crew", "dog", "eon"};  

List<String> wordList = Arrays.asList(words);  

for (String e : wordList)  
{  
     System.out.println(e); 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.