Link to home
Start Free TrialLog in
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".




Avatar of Kryp
Kryp
Flag of United Kingdom of Great Britain and Northern Ireland image

> 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...
Avatar of Fallen_Knight
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!
>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'.
ASKER CERTIFIED SOLUTION
Avatar of Fluffy_Checkers
Fluffy_Checkers

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 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
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);
}
[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);