2D array of strings - c

I'm trying to set up a search for names in a code, so I'm doing a 2D array to store the names. however I'm not getting the desired output from doing this...
#include <stdio.h>
#include <string.h>
main ()
{
char name [4][20], string [20];
int count;
for (count = 0; count <3; count ++)
{
printf ("Enter your name \n ");
scanf ("%s", &string);
strcpy (name [count], string);
}
for (count = 0; count <3; count ++)
{
printf ("%s \n \n");
}
return 0;
}

#include <stdio.h>
#define N_NAME 4
#define NAME_LENGTH 20
int main ()
{
char names[N_NAME][NAME_LENGTH];
// input
int i;
for(i = 0; i<N_NAME ; i++) {
printf("Enter your name: ");
scanf("%s", names[i]);
}
// output
for(i = 0; i<N_NAME ; i++) {
printf("%s\n", names[i]);
}
return 0;
}
Please check this. It should be what you're looking for.
Few things to point out in your original code:
There's no need for "string" since you've stored what you need in the 2D char array.
Use "i" instead of count, which is better for writing clean code.
To make the %s in printf work, you have to give corresponding variable (pointer).

Try this.
#include <stdio.h>
#include <string.h>
main ()
{
char name [4][20], string [20];
for (int row = 0; row <4; row ++)
{
printf ("%s \n \n");
for (int col = 0; col <20; col ++)
{
printf ("Enter your name \n ");
scanf ("%s", &string);
strcpy (name [row][col], string);
}
}
return 0;
}

Related

Array of Pointers - temporary array being created at the same location which changes the previous saved content value

I am trying to write a program that could take in the input (First Name, Last Name, Score) from the user and print it after taking all the inputs like this.
fname1 lname1 : 99
fname2 lname2 : 23
fname3 lname3 : 29
//Note that all of them are input
I wrote the following program, it works but prints the last input n times. I know what the problem is but don't know how to solve it. The temp arrays variable is being created at exactly the same place every time and it is making all the elements the same. How to fix it?
#include <stdio.h>
#define LENGTH 3
int main() {
int *score[LENGTH];
char *fname[LENGTH];
char *lname[LENGTH];
for(int i = 0; i < LENGTH; i++){
printf("Enter name and score of batter %d: ",i);
char ftemp[10];
char ltemp[10];
int tempScore;
scanf("%s %s %d", ftemp, ltemp, &tempScore);
fname[i] = ftemp;
lname[i] = ltemp;
score[i] = &tempScore;
}
for(int i = 0; i < LENGTH; i++){
printf("%s %s %d\n", fname[i], lname[i], *score[i]);
}
}
I think you are trying to assign a reference to a local variable to score,fname and lname .You can not do that because it destroys when exit from their scope,instead of try this
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LENGTH 3
int main() {
int *score[LENGTH];
char *fname[LENGTH];
char *lname[LENGTH];
for(int i = 0; i < LENGTH; i++){
printf("Enter name and score of batter %d: ",i+1);
char ftemp[10];
char ltemp[10];
int tempScore;
scanf("%s %s %d", ftemp, ltemp, &tempScore);
fname[i] = malloc(strlen(ftemp) + 1);
lname[i] = malloc(strlen(ltemp) + 1);
score[i] = malloc(sizeof(int));
if(fname[i]==NULL||lname[i]==NULL||score[i]==NULL)
{
if(fname[i]!=NULL)
free(fname[i]);
if(lname[i]!=NULL)
free(lname[i]);
if(score[i]!=NULL)
free(score[i]);
printf("Memory Error");
exit(1);
}
else
{
strcpy(fname[i],ftemp);
strcpy(lname[i],ltemp);
*score[i]=tempScore;
}
}
for(int i = 0; i < LENGTH; i++){
printf("%s %s %d\n", fname[i], lname[i], *score[i]);
}
}

fgets() in c doesn't read the sentence at all? [duplicate]

This question already has answers here:
Program is skipping fgets without allowing input
(2 answers)
scanf() leaves the newline character in the buffer
(7 answers)
Closed 3 years ago.
I am very new to C. Trying to practice with input and output now. I am trying to use fgets to read in a sentence and convert it to all upper case letters, but for some reason it's not reading it. I HAVE ADDED *c in MY SCANF AND IT STILL DOES NOT WORK. Any suggestions and tips are greatly appreciated! Thanks in advance.
#include <stdio.h>
#include <ctype.h>
#define ARR_SIZE 100
void modify(int pInt[100], int input);
void modify2(char *pString[100]);
int main() {
printf("Enter a size for your array:\n");
int input;
scanf("%d", &input);
printf("Great! You now have an array of size %d.\n", input);
int array[ARR_SIZE];
printf("Now please enter %d numbers.\n", input);
int i;
for (i = 0; i < input; i++) {
scanf("%d*c", &array[i]);
}
printf("Awesome. All %d slots in the array are now filled.\n", input);
printf("Here is how your array looks:\n");
int j;
for (j = 0; j < input; j++) {
printf(" %d |", array[j]);
}
printf("\n");
modify(array, input);
printf("Now your array looks like this:\n");
for (j = 0; j < input; j++) {
printf(" %d |", array[j]);
}
printf("\n");
printf("Experiment #2:\n");
printf("Enter a sentence:\n");
char sentence[ARR_SIZE];
fgets(sentence, ARR_SIZE, stdin);
modify2(sentence);
printf("Now your sentence is upper case: %s\n", sentence);
return 0;
}
void modify2(char *pString[100]) {
int i = 0;
while (pString[i]) {
pString[i] = toupper(pString[i]);
i++;
}
}
void modify(int pInt[100], int input) {
int i;
for (i = 0; i < input; i++) {
pInt[i] *= 2;
}
}

Can I put a for loop inside scanf?

I want to have unknown amount of inputs in a single line. For example, user can input:
"ans: 1 2 3 4 5"
and scanf() will store these five numbers to an array. The problem is that the program don't know how many input will there be.
#include <stdio.h>
int main()
{
int i;
int input[4];
scanf("ans: " for(i = 0, i < 3,i++){scanf(" %d", &input[i]);};
return 0;
}
Sorry I'am totally new to coding, what will be the proper way to write this? Or is this impossible?
Thanks :)
Use fgets() and sscanf() with "%n"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
char input[100];
int arr[10];
//fgets(input, sizeof input, stdin);
strcpy(input, "1 2 42 56 -3 0 2018\n"); // fgets
char *pi = input;
int tmp, pp, i = 0;
while (sscanf(pi, "%d%n", &tmp, &pp) == 1) {
if (i == 10) { fprintf(stderr, "array too small\n"); exit(EXIT_FAILURE); }
pi += pp;
arr[i++] = tmp;
}
printf("got this ==>");
for (int k = 0; k < i; k++) printf(" %d", arr[k]);
puts("");
}
You asked this question way round.
You can achieve what you expect by putting scanf inside of a loop.Even you can ask user to give how many inputs he want to enter.
#include <stdio.h>
int main()
{
int i;
int input[4];
printf("Enter the number of inputs you want to give : ");
scanf("%d", &n);
for(i = 0; i < n;i++)
{
printf("Enter the input number %d : ",i);
scanf("%d", &input[i]);
}
return 0;
}

How can I store String value in char array in C?

I have char array to store string values. I wanted to store the value of a string variable into the char array.
char Password[30];
char User[2];
int i;
for(i=0; i<5; i++) {
printf("Enter Password");
scanf("%s", Password);
strcpy(User[i],Password,30);
}
I wanted to input the values for the array and it should throw a buffer overflow but I couldn't do it. How can I do it?
This should work for you:
#include <stdio.h>
#include <string.h>
int main() {
char Password[30];
char User[5][30];
int i;
for(i = 0; i < 5; i++) {
printf("Enter Password");
scanf("%s", Password);
strcpy(User[i],Password);
}
for(i = 0; i < 5; i++)
printf("Password %d: %s\n", i+1, User[i]);
return 0;
}
The second for loop is to show the output and that every thing is stored right!

assigning variable strings too an array with C

The goal of my code is to have the user, put in any amount of students as an integer and then have the program ask over and over to set a name too every integer (student)
I've been trying so many different things and I've been working on this without using any outside help for hours but I just couldn't figure this out. (if its something obvious, please don't get supermad, I'm only a beginner)
#include <cs50.h>
#include <string.h>
#include <stdio.h>
int main (void)
{
printf("How many students are there? ");
int amount = atoi(GetString());
printf("amount = %i\n", amount);
char *names[amount];
for(int i = 0; i < amount; i++)
{
printf("Enter the ellement #%d :", i +1);
scanf("%s", names[i]);
}
for (int i = 0; i == 0;)
{
printf("Acces student: ");
string search = GetString();
int searchnr = atoi(search);
printf("Student #%d is %s\n", searchnr, names[searchnr]);
}
}
>
}
The obvious solution:
for (int i = 0; i < amount; i++) {
printf("Enter element #%d: ", i + 1);
names[i] = GetString();
}
As to the second loop: it's an infinite loop. What is the terminating condition? You need to put that into the condition of the for loop else it will never terminate.
If your intent is getting an infinite loop, then a more readable, less confusing, more idiomatic solution is
while (1) {
// ...
}
or
for (;;) {
// ...
}
You need to reserve space for those strings:
char *names[amount];
char s[100];
for(int i = 0; i < amount; i++)
{
printf("Enter the ellement #%d :", i +1);
scanf("%s", s);
names[i] = strdup(s);
}
or
char *names[amount];
char s[100];
size_t len;
for(int i = 0; i < amount; i++)
{
printf("Enter the ellement #%d :", i +1);
scanf("%s", s);
len = strlen(s);
names[i] = malloc(len + 1);
strcpy(names[i], s);
}
And this for loop:
for (int i = 0; i == 0;)
does nothing, what do you want to do? (if you want to loop forever you can use for(;;))
Using malloc
char temp[50];
for(int i = 0; i < amount; i++)
{
printf("Enter the ellement #%d :", i +1);
scanf("%s", temp);
names[i]=malloc(strlen(temp)+1);
strcpy(names[i],temp);
}
Using strdup
char temp[50];
for(int i = 0; i < amount; i++)
{
printf("Enter the ellement #%d :", i +1);
scanf("%s", temp);
names[i] = strdup(temp);
}
All your answers were great guys, but this was the final solution:
#include <cs50.h>
#include <string.h>
#include <stdio.h>
int main (void)
{
printf("How many students are there? ");
int amount = atoi(GetString());
char *names[amount];
for(int i = 0; i < amount; i++)
{
printf("Enter the ellement #%d :", i +1);
names[i + 1] = GetString();
}
for (int i = 0; i == 0;)
{
printf("Acces student: ");
int search = atoi(GetString());
printf("Student #%d is %s\n", search, names[search]);
}
}
and I know I have an infinite loop there, that was just temporary so I don't have to rerun the command all the time.

Resources