Solved

Very Simple Seperation

Posted on 1998-01-06
5
169 Views
Last Modified: 2010-04-02
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
Comment
Question by:singhtaj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 10

Expert Comment

by:rbr
ID: 1256928
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
 

Author Comment

by:singhtaj
ID: 1256929
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
 
LVL 10

Accepted Solution

by:
rbr earned 40 total points
ID: 1256930
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
 
LVL 84

Expert Comment

by:ozo
ID: 1256931
#include <string.h>

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

0
 
LVL 1

Expert Comment

by:szetoa
ID: 1256932
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

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question