Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic 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.

Register Log in

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);
}
 

btbass

Advanced Member level 5
Joined
Jul 20, 2001
Messages
1,897
Helped
438
Reputation
880
Reaction score
287
Trophy points
1,363
Location
Oberon
Activity points
12,861
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;
}
 

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
can u jus do it only using stdio.h lib??
coz im only most familiar wit that lib..
=)
 

btbass

Advanced Member level 5
Joined
Jul 20, 2001
Messages
1,897
Helped
438
Reputation
880
Reaction score
287
Trophy points
1,363
Location
Oberon
Activity points
12,861
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.
 

ahmon_abilar

Junior Member level 1
Joined
Mar 25, 2008
Messages
15
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,281
Activity points
1,349
#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.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top