Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

C problem.. urgent need of help

Status
Not open for further replies.

dck

Junior Member level 1
Joined
Aug 6, 2008
Messages
18
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,411
how can i store strings into an array and display it???

here is the half-way program.. i got stucked..
thnx...

#include<stdio.h>
main()
{
char name[50];
int i;

for(i=0;i<5;i++)
{
printf("Please enter name:");
gets(name);
}

for(i=0;i<5;i++)
printf("%s\n", name);
}
 

Here is a version but it lacks error checking.


Code:
#include<stdio.h>
#include<stdlib.h>
#include <string.h>

int main(void)
{
char buffer[50];
char *name[5];
int i; 

for(i = 0; i < 5; i++)
  {
  printf("Please enter name:");
  gets(buffer);
  name[i] = malloc(strlen(buffer));
  strcpy(name[i], buffer);
  }

for(i = 0; i < 5; i++){
  printf("%s\n", name[i]);
  }

return 0;
}
 

can u jus do it only using stdio.h lib??
coz im only most familiar wit that lib..
=)
 

They are all part of the standard c library, they will exsist in your compiler.
If you are going to use strings, it is worthwhile looking at the functions in <string.h>

You could do it using a 2 dimension array,
name[5][50];
But this will waste a lot of memory.
 

#include<stdio.h>
main()
{
char name[50];
int i;

printf("Please enter name:");
scanf("%s", &name);

printf("%s", name);
}

Added after 2 minutes:

#include<stdio.h>
main()
{
char name[5][50];
int i;

for(i=0; i<5; ++i)
{
printf("Please enter name:");
scanf("%s", &name);
}

for(i=0; i<5; ++i)
{
printf("%s", name);
}
}
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top