?
Solved

String problem

Posted on 2003-02-21
7
Medium Priority
?
253 Views
Last Modified: 2010-04-15
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".




0
Comment
Question by:Junster
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 2

Expert Comment

by:Kryp
ID: 7994699
> 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...
0
 

Expert Comment

by:Fallen_Knight
ID: 7996952
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!
0
 

Expert Comment

by:foxroom2
ID: 7996958
>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'.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Accepted Solution

by:
Fluffy_Checkers earned 75 total points
ID: 7996988
Here you go....shows a few different things....hope this helps...

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

void Question1()
{
     char str1[64], *str2;

     printf("QUESTION 1\n");
     strcpy(str1, "Hello Fluffy");
     
     printf ("%s\n", str1);
     
     str2 = strrev(str1);

     printf ("%s\n", str2);
}

void Question2()
{
     char needle[64] = "dog";
     char haystack1[64] = "cat lion bird dog emu";
     char haystack2[64] = "cat lion bird";
     char *location;

     printf ("QUESTION 2\n");
     location = strstr(haystack1, needle);
     printf("Seaching for <%s> in <%s>\n", needle, haystack1);
     if (location == NULL) printf ("Couldn't find it\n");
     else printf("Found it\n");

     location = strstr(haystack2, needle);
     printf("Seaching for <%s> in <%s>\n", needle, haystack2 );
     if (location == NULL) printf ("Couldn't find it\n");
     else printf("Found it\n");

     
}

int InsertAtLoc(char *str, int location, char *substring)
{
     char *temp;

     temp = (char *)malloc(64); //Grab some memory
     if (temp == NULL) return -1;

     strncpy(temp, str, location);
     strcpy(&temp[location],substring);
     strcat(temp," "); //need to add another space
     strcat(temp, &str[location]);
     printf("<%s>\n", temp);

     free(temp);
     return 0;
}

void Question3()
{
     char str[64] = "reverse word";

     printf("QUESTION3\n");

     printf("%s\n", str);
     InsertAtLoc(str, 8, "this");

}

void main(void)
{
     Question1();
     Question2();
     Question3();
}



0
 

Author Comment

by:Junster
ID: 7997826
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
0
 

Expert Comment

by:Fallen_Knight
ID: 7998014
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);
}
0
 
LVL 1

Expert Comment

by:umangjoshi
ID: 7998456
[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);
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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 how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

770 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