Avatar of Junster
Junster
 asked on

String problem

I have a few questions on string.i don't really know how to use the functions in the string library.

1)how to reverse words in string?
Eg:i input this string "reverse word" and the output will be "esrever drow"

2)how to look for substring in string?
i will enter a substring that i want and display a message to state if the word is in the string.
eg:i want to find "word" in "reverse word" and a message will be displayed saying that the substring is found in the string.

3)how to insert substring into string
i will enter a substring and the position where i want to insert it.Then the new string will be displayed on the screen.
eg:i enter "this" and i want to put in between "reverse" and "word".The new string will be " reverse this word".




C

Avatar of undefined
Last Comment
umangjoshi

8/22/2022 - Mon
Kryp

> 1)how to reverse words in string?
Find the \0 at the end and work your way back to the start
Finding \0 is in effect what strlen() does.

See how you get on with problem 1 first...
Fallen_Knight

1) reverseing individual words is a bit tricky, one way to do this would be to replace all the spaces with '\0', call strrev() on the char rigth after each '\0', and the start of the string, but not the last '\0'. you'd have to keep track of the str length as not to go past the real '\0'.

2) use strstr, it does exactly that

3) well you'd have to either have enough room in your inital string or allocate a new string.

like

//returns newly allocated memory
char *insertstr(char *str1, char *str2, int pos) {
    char *newstr, tmp;

    newstr = malloc(strlen(str1)+strlen(str1)+1);
    tmp = *(str+pos-1);
    *(str1+pos-1) = '\0';
    strcpy(newstr, str1);
    strcpy(newstr+strlen(newstr)-1, str2);
    *(str+pos-1) = tmp;
    strcpy(newstr+strlen(newstr)-1, str1+pos-1);

   return(newstr);
}

Hope that helps!
foxroom2

>2)how to look for substring in string?

char string1[] = "Got food?";
char string2[] = "foo";

if (strstr (string1, string2))
  printf("'%s' is a substring of '%s'.\n", string2, string1);

>3) it seems C doesn't have stuff to do this directly, as i know, so have to do it yourself. also check out "strchr" and other func in 'string.h'.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ASKER CERTIFIED SOLUTION
Fluffy_Checkers

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Junster

ASKER
thank you for the coding for q 2 and 3.i managed to get the answers. but for q1, i wanted to reverse the characters for individual word.do u mean that i should use for loop to find '\0' then reverse the word?

Eg:

The gate to Java nirvana is near

the method outputs

ehT etag ot avaJ anavrin si raen
Fallen_Knight

ok, well heres is what i exaplined in code, untested of course, so your going to have to debug it. but this is the general solution

char *reverse words(char *str) {
    int len, i;
    char *cp;
   
    cp = str;
    while( *cp != '\0') {
        if(*cp == ' ') {
            *cp = '\0';
        }
        cp++;
    }

    len = strlen(str);
    for(i = 0, cp = str; i < len; i++) {
        if(*(cp+i) == '\0') {
            strrev((cp+i+1));
            *(cp+i) = ' ';
        }
    }

    return(str);
}
umangjoshi

[1] reverse a word

char str = "abcd";
strrev(str);
printf("%s",str);

[2]earch a string
char str[] = "please solve my problem";
char word[] = "solve";

char *ptr;

ptr = strstr(word,str);
if (ptr != null)
{
 printf("word found");
}

[3] how to insert a word

char str1[] = "abcd";
char str2[] = "pqr";
char word[] = "ooo";

char str[100];
strcpy(str,str1);
strcat(str,word);
strcat(str,str2);
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.