[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 173
  • Last Modified:

Very Simple Seperation

If I have a program as follows :
-----
include <stdio.h>
#include <stdlib.h>

void main ()
{
 char *xyz[3]={"abc"} ;

 printf ("\nThis is the string : %s ",xyz[2]);

}
---------------
How would I access the letter "b" in my string xyz.
The above does not work.
Any help will be appreciated.
0
singhtaj
Asked:
singhtaj
1 Solution
 
rbrCommented:
Use char *xyz="abc";
or char xyz[4] = "abc";

You have to use 4 since a "" string conetains a \0 at the end

or use
char xyz[3]={'a','b','c'}; without the \0
0
 
singhtajAuthor Commented:
char *xyz  could contain any data.

for example if xyz contains "list"

then how would I access "l"

That is


lets say the program is :
 
#include <stdio.h>
#include <stdlib.h>

             void main ()
             {
              char *xyz ;
  printf("\nEnter String :");
  scanf("%s",xyz)
              printf ("\nThis is the string : %s ",xyz[2]);

             }

=====

Now if the user enters "list" and I want to access "s" How
Would I do it ????


0
 
rbrCommented:
You have to make an array that is large enough to contain the data.

char xyz[100];

scanf ("%s",xyz);

if (strlen(xyz)>2)
   printf ("\nThis is in the string: %c",xyz[2]);

0
 
ozoCommented:
#include <string.h>

char *xyz = strchr("list",'s');

0
 
szetoaCommented:
If I understand your question correctly, you want to print out
the character 'b' in the array xyz.  You should use %c in the
printf() statement, not %s.  Also the letter 'b' is in subscript
1, not 2.

    printf("\nThis is the string %c\n", xyz[1]);

If you want to start printing from the second character and on,
do this:

    printf("\nThis is the string %s\n", & xyz[1]);

If you want to see if 'b' is in the string, ozo's way is correct.
Or you can check in this way:

    if ( strchr( xyz, 'b' ))
        /* found it */
    else
        /* not found */

Hopt this help.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now