how to get input from user in - int main()

I am looking this problem.
i tried but not successful.
How to get input from user in this program, in the int main() function.

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
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>
#include <string.h>

// The pointer version.
void reverse1 (char *str) 
{
    char t;                      // Temporary char for swapping.
    char *s = str;               // First character of string.
    char *e = &(s[strlen(s)-1]); // Last character of string.
    
    // Swap first and last character the move both pointers
    // towards each other. Stop when they meet or cross.
    while (s < e) {
	t = *s;
	*s++ = *e;
	*e-- = t;
    }
}

// The array version.
void reverse2 (char *str) {
    char t;                // Temporary char for swapping.
    int s = 0;             // First character of string.
    int e = strlen(str)-1; // Last character of string.
    
    // Swap first and last character the move both pointers
    // towards each other. Stop when they meet or cross.
    while (s < e) {
	t = str[s];
	str[s++] = str[e];
	str[e--] = t;
    }
}

int main (void) {
    char x[] = "This is a string for reversing.";
    printf ("Original: [%s]\n", x);
    reverse1 (x);
    printf ("Reversed: [%s]\n", x);
    reverse2 (x);
    printf ("Again: [%s]\n", x);
    return 0;
}


Please tell me how to do it.
Last edited on
Look at cin.
Have you tried scanf()? Specifically, have you tried something along the lines of the first instance of scanf() shown in the following example?

http://www.cplusplus.com/reference/clibrary/cstdio/scanf/

You may need to scroll down a bit.

Good luck!

-Albatross

EDIT: I'm assuming you're using C.
Last edited on
a simple way...
int main()
{
const int size=10;
int c=0;
char x[size];
int count=0;
for(;count<size;count++)
{
c=getchar();
x[count]=c;
}
//......
return 0;
}
You want to input a string, you can use gets, or fgets. fgets is recommended.


~Gorav
http://www.kgsepg.com
Topic archived. No new replies allowed.