473,387 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Help on code that compares strings

10
I am writting that code that takes in two strings and compares them, then outputs a new string.

It goes like this

Hello their. How are you?
Hel

lo their. How are you?


Now I have a replace function, and string match function and a driver that takes in two strings. Everytime I take in two strings, it outputs some weird stuff. I have no idea what is up with it.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int sreplace(char newc, char oldc, char *s)
  4. {
  5.         int i;
  6.         int cnt;
  7.         int len = strlen(s);
  8.  
  9.         i = cnt = 0;
  10.  
  11.         while(i<len) //loop to run till the end of the string
  12.         {
  13.                 if(s[i] == oldc) //if the char found which to be replaced
  14.                 {
  15.                         s[i] = newc; //replace
  16.                         cnt++;
  17.                 }
  18.                 i++;
  19.         }
  20.         return cnt;
  21. }
  22.  
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. char *strmatch(char *str, char *s)
  4. {
  5.         int len1;
  6.         int len2;
  7.         char *indxptr;
  8.         int i;
  9.         int j;
  10.         int k;
  11.         int cnt;
  12.  
  13.         i = j = k = cnt =0;
  14.  
  15.         len1 = strlen(str); //length of string 1
  16.         len2 = strlen(s);  //length of string 2
  17.  
  18.         while(i<len1)
  19.         {
  20.                 if(str[i] == s[0])
  21.                 {
  22.                         k = i; //saves state of i
  23.                         while(j<len2) //loop to check till end of string 2
  24.                         {
  25.                                 if(str[i] == s[j]) //if still equal
  26.                                 {
  27.                                         i++;
  28.                                         indxptr = &str[i];
  29.                                         cnt++;
  30.                                         if(cnt == len2)
  31.                                                 return indxptr;
  32.                                 }
  33.                                 else
  34.                                 {
  35.                                         indxptr = NULL;
  36.                                         j = 0;
  37.                                         i = k;
  38.                                         cnt = 0;
  39.                                         break;
  40.                                 }
  41.                                 j++;
  42.                         }
  43.                 }
  44.                 i++;
  45.         }
  46.         return indxptr;
  47. }
  48.  
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. #include "strmatch.c"
  4. #include "sreplace.c"
  5.  
  6. int main()
  7. {
  8.         char *line1;
  9.         char *line2;
  10.  
  11.         int c;
  12.  
  13.         line1 = (char *) malloc(1024);
  14.         line2 = (char *) malloc(1024);
  15.  
  16.         char *indxptr;
  17.  
  18.         int rep1;
  19.         int rep2;
  20.  
  21.         while((c = getchar()) != EOF)
  22.         {
  23.         fgets(line1,1024,stdin);
  24.  
  25.         fgets(line2,1024,stdin);
  26.  
  27.         rep1 = sreplace('\0','\n', line1);
  28.         rep2 = sreplace('\0','\n', line2);
  29.  
  30.         if(indxptr == NULL) //if not match
  31.                 return -1;
  32.         else
  33.  
  34.         printf("%s\n",indxptr);//if found
  35.         }
  36.         getchar();
  37.  
  38.  
  39.         return 0;
  40. }
  41.  
  42.  
ANY suggestions?
Sep 20 '07 #1
1 1107
RRick
463 Expert 256MB
You are printing out the string pointed to by indxptr. The problem is that indxptr has never been initialized, and is pointing to who knows what. That's the garbage you're printing out. Your program could also crash and burn with some indxptr values.

It looks like you want indxptr to hold the value returned by strmatch, but never call strmatch. Don't be fooled with the definition of indxptr inside strmatch. That indxptr is different from the indxptr in main. Once again, if you want indxptr in main to contain a value, you have to set it to one.

With pointers, its best if you initialize them with 0 or some real pointer value. If you had, then your logic in main would have caught the problem.
Sep 20 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Greg Baker | last post by:
I don't know what standard protocol is in this newsgroup. Am I allowed to post code and ask for help? I hope so.. :) Here's my problem: I am trying problem 127 of the valladolid online...
4
by: Lorin Leone | last post by:
Can anyone help me modify the program so that it recognizes strings like "Anna" as palindromes. To make the program "case-insensitive." using the built-in C++ function "toupper". and so that it...
7
by: Alan Bashy | last post by:
Please, guys, In need help with this. It is due in the next week. Please, help me to implement the functions in this programm especially the first three constructor. I need them guys. Please, help...
8
by: SpOiLeR | last post by:
Hi there... I have something like this: #include <string> #include <list> #include <algorithm> using namespace std; typdef list<string> ls;
8
by: PengHQ | last post by:
int compare(const void *x,const void *y) { strcmp(*(char **)x,*(char **)y); } why not code like these: int compare(const void *x,const void *y) { strcmp(x,y); }
18
by: Nobody | last post by:
I've been looking for a job for a while now, and have run into this interview question twice now... and have stupidly kind of blown it twice... (although I've gotten better)... time to finally...
0
by: CoreyWhite | last post by:
I've unlocked the real Davinci Code, and it turns out it is an alphabetically backwards suffix sort. I have the source code printed out here in both C++ and PHP. I devised the algorithm after...
2
by: Timmy | last post by:
The bigger problem is with the Binary Search. The program crashes when it's excuted. and Visual Studio 2005 indicates stack over flow and shows a break at that function. Sequential search is...
17
by: madeofdata | last post by:
I need the code to implement those two functions; myStrCmp Compares two strings, returns true if equal and false otherwise int myStrCmp(char* str1, char* str2) myStrNCpy Copies N characters...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.