?
Solved

Very Simple Seperation

Posted on 1998-01-06
5
Medium Priority
?
171 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 160 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Suggested Courses

764 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