Solved

Very Simple Seperation

Posted on 1998-01-06
5
165 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
5 Comments
 
LVL 10

Expert Comment

by:rbr
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
#include <string.h>

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

0
 
LVL 1

Expert Comment

by:szetoa
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 recursion in the C programming language.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now