JoC
JoC

Reputation: 253

Shorten C string from last occurence of a char?

So what I wish to do is cut a string from the last occurence of a char. For e.g

input =  "Hellomrchicken"
input char = "c"
output = "cken"

Problem is I cannot get the count to work, and because of that i cannot test out the logic. I wish to use a pointer to do so, and theoretically i would test if the Content inside the pointer is == to a null value. I used a while loop here. Any help is appreciated thanks!

#include <stdio.h>
#include <stdlib.h>

char *stringcutter(char *s, char ch);
int count( char *s);

void main(){
    char input[100];
    char c;
    printf("Enter a string \n");
    gets(input);
    printf("Enter a char \n");
    scanf("%c", &c);
    stringcutter( *input , c );
    getchar();
    getchar();
    getchar();
}


char *stringcutter(char *s, char ch){
    int count = 0;
    // Count the length of string

            // Count the length of string
while ( check != '\0'){
            count++;
            s++;
            printf("Processed");


    printf("TRANSITION SUCCESSFUL /n");
    printf( "Count = %d /n" , count);


    // Count backwards then print string from last occurence

/*  for (i=count ; i != 0 ; i--){
        if (str[i] == ch)
            *s = *str[i];
        printf ( "Resultant string = %s", *s )
            */
            return 0; 
    }

Sorry dont know why the code got cut off halfway

Upvotes: 2

Views: 351

Answers (3)

Pankrates
Pankrates

Reputation: 3095

The original post does not make it really clear if you want to define this function from scratch but it exists in string.h and it would look something like

#include <stdio.h>
#include <string.h>

int main ()
{
    char input[] = "Hellomrchicken";
    char c = 'c';
    char *p;
    p = strrchr(input, c);
    printf("Last occurence of %c found at %d \n", c, p-input+1);
    return 0;
}

Upvotes: 4

Adrian Ratnapala
Adrian Ratnapala

Reputation: 5723

The strrchr() function does this for you.

char *output = strrchr(string_to_search, char_to_find);
int output_index = (output == NULL ? ERROR : output - string_to_search);

If you want to do it by hand then (in c99 syntax)

char *output = NULL;
for(char p = string_to_search; *p; p++)
    if(*p == char_to_find)
        output = p;

Upvotes: 0

Henrik
Henrik

Reputation: 4314

When working with strings in C, we generally use what's known as C strings or '\0' terminated strings. These are just continuous sequences of chars, terminated with the char '\0', a 0-byte.

Because of this, a way of traversing a string that is idiomatic to C is the following

char *my_string = "Hello, world";

char *p = my_string;
while (p != '\0')
{
    /* Do some work */

    p++;
}

You can use a loop like this to get a pointer to the last occurrence of a specific character.

char *from_last_instance_of(char *input, char c)
{
    char *last_instance_of_c = input;
    while (input != '\0')
    {
        if (*input == c)
            last_instance_of_c = input;

        input++;
    }
    return last_instance_of_c;
}

As you see, all of the work is done in place. If you want to copy the string before manipulating it further, use strcpy to copy from the location given by the returned pointer.

Upvotes: 1

Related Questions