how to memorize n character strings
Jan 9, 2013 at 7:47am UTC
I have to write a program that reads from the keyboard an integer n, and then reads n character strings. And I have to print the common symbols of all the strings. And I really have no ideea how to keep in memory the old strings.
I just did the reading part.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#include <stdio.h>
void citire (unsigned int n)
{ char sir[11];
while (n>0)
{
scanf("%10s" ,sir); //reads n strings
n--;
}
}
void main ()
{
unsigned int n;
scanf("%d" ,&n);
citire (n);
}
Jan 9, 2013 at 8:04am UTC
use an array or a vector
Jan 9, 2013 at 8:05am UTC
Something like that?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#include <stdio.h>
#include <cstring>
void citire(unsigned int n)
{
const int stringlimit = 100;
char ** strings = new (char *[stringlimit]);
char temp[11];
int x = 0;
while (x<n)
{
scanf("%10s" ,temp);
strings[x] = new char [strlen(temp)];
strcpy(strings[x], temp);
++x;
}
for (x = 0; x < n; ++x) {
printf("%s\n" ,strings[x]);
}
}
int main()
{
unsigned int n;
scanf("%d" ,&n);
citire (n);
}
Topic archived. No new replies allowed.