Solved

Very Simple Seperation

Posted on 1998-01-06
5
167 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
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Converting a float to a string function in c languange 10 206
Test against App 49 137
Can case within switch statement specify range of values ? 3 81
Acrinis True image 2 77
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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers 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.

785 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