Hi guys! I just wanna ask on how to access the first character in a string?

Recommended Answers

All 11 Replies

Just refer to it as mystr[0].

Just refer to it as mystr[0].

dude it's not working for me. For example i enter a char value 34, now i can access the 4 by using for ex. mystr[1] but when it comes to accessing 3 by using this mystr[0] it always printing 34. I have no idea why it always given me that output.. need help guys.

I wrote up this simple test program:

#include <stdio.h>

int main()
{
   char number[12];

   printf("Enter a number: ");
   fgets(number, 12, stdin);

   printf("The first digit is: %c\n", number[0]);
   return 0;
}

and it works for me.
Note that if you declare the char array with a pointer, like:

char *number;

the first element acts as a pointer to the whole array. Thus, referring to number[0] in this case would be referring to the first point in memory, which is in itself a pointer to the whole array, thus printing the whole thing.
If you declare it as an array as I did, then you are actually dereferencing it by referring to number[0], thus it only prints that byte in memory, and not the value pointed to by the first byte.

Thanks dude! Last question, what if i use integer instead of char?

Did you want to access the first integer in a string?
or the first character in an integer? :-)

How about the first integer in an array of integers?

int myarray[10] = {1,2,3,4,5,6,7,8,9,10};
    printf("%d\n", myarray[0]);
    printf("%d\n", *myarray);
    printf("%d\n", myarray[5]);
    int *p;
    p = myarray;
    printf("%d\n", *p++);
    printf("%d\n", *p++);
    p = &(myarray[8]);
    printf("%d\n", *p++);
    printf("%d\n", *p++);
    printf("%d\n", *p++);

Compile it, run it, and examine the output. Fix at least one (intentional) error.

Still doesn't work for my code dude.. I think there's something wrong about my code but i can't figured it out. Here's my code:

char two_digit[2];
        printf("Enter 2 digit number: ");
	gets(two_digit);
        CtoI2 =(int)atoi(&two_digit[0]);
        printf("two digit %d\n",CtoI2);

when i enter a value of 34 and i want to access 3 by using two_digit[0]
the output is still 34. Hope you can help me with this.

Here is the code I wrote up to solve your problem.

#include <stdio.h>
#include <stdlib.h>

main()
{
   char two_digit[3];
   int digit;
   
   printf("Enter two digits: ");
   fgets(two_digit, 3, stdin); //Accept size-1 (in this case, 3-1 which is 2) bytes into the buffer.
   digit = atoi(&two_digit[0]);
   printf("The first digit is: %d\n", digit);
}

First of all, you need to assign a size of three bytes to two_digit, because the string should be terminated by NULL, which will require one extra byte. It's good practice to assign at least one or two bytes extra, because of how different functions might terminate them.

Second, you should use fgets(buffer, numBytes, fileStream) rather than gets(), because gets() does not place bounds checking on the input. This means that if I ran your program, I would be allowed to enter more than two bytes and it would cause a vulnerability known as a "buffer overflow". gets() is a very unsafe function so I highly advise you avoid using it. Just a tip.

Third, atoi() converts the const char* to an int for you, so you don't need to use the (int) conversion.
This should all work. Just read it and learn from it.

Hi guys! I just wanna ask on how to access the first character in a string?

Ok its my turn :)

first you have types of data in your case you can make it in two ways char and int

char means that you can input strings and numbers kind of symbol but int means integer this mean only numbers

Next stop arrays are data in cells in your memory the name of the array point the first item of your array

so here we are up to:

#include <stdio.h>

int main(void)
{
   int arr[5] = {1,2,3,4,5} // declaring your array with name arr with restriction/limit of 5 int in it.     The count starts from 0 so here is it!
   int i= 0;
   printf("First element of my array is: %d", arr[0]); // if you wanna do it with var so here how it will look like;

   printf("My %d elemnt of my array is: %d", i,arr[i]);

//dont forget that the count in arrays starts from 0 so in your case arr[0] must be equal to 1  arr[1] = 2, arr[2] = 3.......


//hope you get it :)

   return 0;
}

Happy and peaceful coding :)

Here is the code I wrote up to solve your problem.
c Syntax (Toggle Plain Text)
#include <stdio.h>
#include <stdlib.h>
 
main()
{
   char two_digit[3];
   int digit;
 
   printf("Enter two digits: ");
   fgets(two_digit, 3, stdin); //Accept size-1 (in this case, 3-1 which is 2) bytes into the buffer.
   digit = atoi(&two_digit[0]);
   printf("The first digit is: %d\n", digit);
}

What compiler did you use for this dude? I use turbo c and when i run your program i still get two values.

This is Turbo C guaranteed! ;)

/*Here is the code I wrote up to solve your problem.
c Syntax (Toggle Plain Text)
*/

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
   char two_digit[3];
   int digit;
 
   printf("Enter two digits: ");
   fgets(two_digit, 3, stdin); //Accept size-1 (in this case, 3-1 which is 2) bytes into the buffer.
   digit = two_digit[0] - '0';  //zero, not Oh
   printf("The first digit is: %d\n", digit);

   printf("\n\n\t\t\t    press enter when ready");
   while((digit=getchar()) != '\n');
   digit=getchar();
   return 1;
}

Thanks so much dude.. now it works. :*

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.