1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: C Code Block Help

Discussion in 'Software Development' started by InuyashaITB, Jul 24, 2009.

Thread Status:
Not open for further replies.
Advertisement
  1. InuyashaITB

    InuyashaITB Thread Starter

    Joined:
    Jun 20, 2009
    Messages:
    20
    I need a program that can take two strings, and determine if the second is in the first....

    i for the love of god cannot figure this one out... ive figured out every other part of my program except this one part and its driving me insane lol...

    So far i have this...

    #include<ctype.h>
    #include<string.h>
    #include<stdlib.h>
    #include<stdio.h>
    #define SWAP_CHAR( x, y ) {char c; c = x; x = y; y = c;}
    void reverse(char t[]);

    int main(void)
    {
    int select, i; //input of user variable
    char sentence[40], sentence2[40];//variable for input sentenceings
    printf("1 Convert string to all uppercase.\n"
    "2 Convert string to all lowercase.\n"
    "3 Append and a million dollars. to the string using strncat().\n"
    "4 Print the string in reverse, then print the original sentenceing.\n"
    "5 Take two strings, and determine if the second is in the first.\n"
    "6 Quit\n");
    while (scanf("%d", &select))
    {
    if (select == 1)
    {
    printf("Please enter a sentenceing to convert to all uppercase: ");
    scanf("%s", sentence);
    for( i = 0; sentence[ i ]; i++)
    sentence[ i ] = toupper( sentence[ i ] );
    printf("Your sentence in all uppercase letters is: %s\n\n", sentence);
    }
    else if(select == 2)
    {
    printf("Please enter a sentenceing to convert to all lowercase: ");
    scanf("%s",sentence);
    for( i = 0; sentence[ i ]; i++)
    sentence[ i ] = tolower( sentence[ i ] );

    printf("Your sentence in all lowercase letters is: %s\n\n", sentence);
    }
    else if(select == 3)
    {
    printf("Type something to add a million dolors to it: ");
    scanf("%s", sentence);
    strcat(sentence, " and a million dollors.");
    printf("%s\n\n", sentence);
    }
    else if(select == 4)
    {
    printf("Enter something to be reversed: ");
    scanf("%s", sentence);
    reverse(sentence);
    printf("Your sentence reversed is: %s\n", sentence);

    reverse(sentence);
    printf("Your original sentence was: %s\n\n", sentence);

    }
    else if(select == 5)
    {
    printf("Input a string: ");
    scanf("%s", sentence);

    printf("\nPut another string in: ");
    scanf("%s", sentence2);

    }
    else if(select == 6)
    {
    exit(1000);
    }
    else
    {
    printf("I do not understand %d please enter a valid number\n", select);
    }
    printf("1 Convert string to all uppercase.\n"
    "2 Convert string to all lowercase.\n"
    "3 Append and a million dollars. to the string using strncat().\n"
    "4 Print the string in reverse, then print the original sentenceing.\n"
    "5 Take two strings, and determine if the second is in the first.\n"
    "6 Quit\n");
    }
    return select;
    }
    void reverse(char t[]) //reverse the characters
    {
    int i,j;
    for(i = 0, j = strlen(t)-1; i < j; i++, j--)
    SWAP_CHAR(t, t[j]);
    }



    This is where im stuck at:

    else if(select == 5)
    {
    printf("Input a string: ");
    scanf("%s", sentence);

    printf("\nPut another string in: ");
    scanf("%s", sentence2);

    }

    Can anyone help me out?
     
  2. JimmySeal

    JimmySeal

    Joined:
    Sep 25, 2007
    Messages:
    290
    Code:
    if(strstr(sentence1, sentence2) != NULL)
    {
         // hooray
    }
    should do the trick.
     
  3. InuyashaITB

    InuyashaITB Thread Starter

    Joined:
    Jun 20, 2009
    Messages:
    20
    Ok, thank you, it worked
     
  4. kylesellers

    kylesellers

    Joined:
    Aug 4, 2009
    Messages:
    4
  5. InuyashaITB

    InuyashaITB Thread Starter

    Joined:
    Jun 20, 2009
    Messages:
    20
    Code:
    #include<ctype.h>#include<string.h>#include<stdlib.h>#include<stdio.h>#define SWAP_CHAR( x, y ) {char c; c = x; x = y; y = c;}void reverse(char t[]); //reverse string functionvoid testing(void); //testing everythin input relatedint main(void){printf("1 Convert string to all uppercase.\n"		   "2 Convert string to all lowercase.\n"		   "3 Append  and a million dollars. to the string using strncat().\n"		   "4 Print the string in reverse, then print the original sentenceing.\n"		   "5 Take two strings, and determine if the second is in the first.\n"		   "6 To invert the case of your string!\n"		   "7 Quit\n\n");	testing();		return 100;	}void reverse(char t[]) //reverse the characters{  int i,j;  for(i = 0, j = strlen(t)-1; i < j; i++, j--)    SWAP_CHAR(t[i], t[j]);}void testing(void){	int select, i; //input of user variable	char sentence[100], sentence2[100];//variable for input sentenceings	char * ptr;	while (scanf_s("%d", &select) != 7)	{				if (select == 1)			{				printf("Please enter a sentenceing to convert to all uppercase:\n");				gets_s(sentence,40);				ptr = gets_s(sentence,40);				for( i = 0; sentence[ i ]; i++)					sentence[ i ] = toupper( sentence[ i ] );												printf("Your sentence in all uppercase letters is: %s\n\n", sentence);			}		else if(select == 2)			{				printf("Please enter a sentenceing to convert to all lowercase:\n");				gets_s(sentence,40);				ptr = gets_s(sentence,40);				for( i = 0; sentence[ i ]; i++)					sentence[ i ] = tolower( sentence[ i ] );								printf("Your sentence in all lowercase letters is: %s\n\n", sentence);			}		else if(select == 3)			{				printf("Type something to add a million dolors to it:\n");				gets_s(sentence,40);				ptr = gets_s(sentence,40);				strcat_s(sentence, 63," and a million dollors.");				printf("%s\n\n", sentence);			}		else if(select == 4)			{				printf("Enter something to be reversed:\n");				gets_s(sentence,100);				ptr = gets_s(sentence,100);				reverse(sentence);				printf("Your sentence reversed is: %s\n", sentence);								reverse(sentence);				printf("Your original sentence was: %s\n\n", sentence);			}		else if(select == 5)			{				printf("Input a string:\n");				gets_s(sentence,40);				ptr = gets_s(sentence,40);							printf("\nPut another string in:\n");				gets_s(sentence2,40);				if(strstr(sentence, sentence2) != NULL){     printf("%s is contained within %s.\n\n", sentence2, sentence);}				else				{					printf("%s is not within the first string.\n", sentence);				}			}		else if(select == 6)		{			printf("Input a string to be inverted:\n");			gets_s(sentence,40);			ptr = gets_s(sentence,40);						for( i = 0; sentence[ i ]; i++)			{				if (sentence[ i ] == 'a')						sentence[ i ] = 'A';				else if (sentence[ i ] == 'b')						sentence[ i ] = 'B';				else if (sentence[ i ] == 'c')						sentence[ i ] = 'C';				else if (sentence[ i ] == 'd')						sentence[ i ] = 'D';				else if (sentence[ i ] == 'e')						sentence[ i ] = 'E';				else if (sentence[ i ] == 'f')						sentence[ i ] = 'F';				else if (sentence[ i ] == 'g')						sentence[ i ] = 'G';				else if (sentence[ i ] == 'h')						sentence[ i ] = 'H';				else if (sentence[ i ] == 'i')						sentence[ i ] = 'I';				else if (sentence[ i ] == 'j')						sentence[ i ] = 'J';				else if (sentence[ i ] == 'k')						sentence[ i ] = 'K';				else if (sentence[ i ] == 'l')						sentence[ i ] = 'L';				else if (sentence[ i ] == 'm')						sentence[ i ] = 'M';				else if (sentence[ i ] == 'n')						sentence[ i ] = 'N';				else if (sentence[ i ] == 'o')						sentence[ i ] = 'O';				else if (sentence[ i ] == 'p')						sentence[ i ] = 'P';				else if (sentence[ i ] == 'q')						sentence[ i ] = 'Q';				else if (sentence[ i ] == 'r')						sentence[ i ] = 'R';				else if (sentence[ i ] == 's')						sentence[ i ] = 'S';				else if (sentence[ i ] == 't')						sentence[ i ] = 'T';				else if (sentence[ i ] == 'u')						sentence[ i ] = 'U';				else if (sentence[ i ] == 'v')						sentence[ i ] = 'V';				else if (sentence[ i ] == 'w')						sentence[ i ] = 'W';				else if (sentence[ i ] == 'x')						sentence[ i ] = 'X';				else if (sentence[ i ] == 'y')						sentence[ i ] = 'Y';				else if (sentence[ i ] == 'z')						sentence[ i ] = 'Z';				else if (sentence[ i ] == 'A')						sentence[ i ] = 'a';				else if (sentence[ i ] == 'B')						sentence[ i ] = 'b';				else if (sentence[ i ] == 'C')						sentence[ i ] = 'c';				else if (sentence[ i ] == 'D')						sentence[ i ] = 'd';				else if (sentence[ i ] == 'E')						sentence[ i ] = 'e';				else if (sentence[ i ] == 'F')						sentence[ i ] = 'f';				else if (sentence[ i ] == 'G')						sentence[ i ] = 'g';				else if (sentence[ i ] == 'H')						sentence[ i ] = 'h';				else if (sentence[ i ] == 'I')						sentence[ i ] = 'i';				else if (sentence[ i ] == 'J')						sentence[ i ] = 'j';				else if (sentence[ i ] == 'K')						sentence[ i ] = 'k';				else if (sentence[ i ] == 'L')						sentence[ i ] = 'l';				else if (sentence[ i ] == 'M')						sentence[ i ] = 'm';				else if (sentence[ i ] == 'N')						sentence[ i ] = 'n';				else if (sentence[ i ] == 'O')						sentence[ i ] = 'o';				else if (sentence[ i ] == 'p')						sentence[ i ] = 'p';				else if (sentence[ i ] == 'Q')						sentence[ i ] = 'q';				else if (sentence[ i ] == 'R')						sentence[ i ] = 'r';				else if (sentence[ i ] == 'S')						sentence[ i ] = 's';				else if (sentence[ i ] == 'T')						sentence[ i ] = 't';				else if (sentence[ i ] == 'U')						sentence[ i ] = 'u';				else if (sentence[ i ] == 'V')						sentence[ i ] = 'v';				else if (sentence[ i ] == 'W')						sentence[ i ] = 'w';				else if (sentence[ i ] == 'X')						sentence[ i ] = 'x';				else if (sentence[ i ] == 'Y')						sentence[ i ] = 'y';				else if (sentence[ i ] == 'Z')						sentence[ i ] = 'z';			}				printf("Your sentence in opposite case is: %s\n\n", sentence);		}		else if(select == 7)			{						exit(1000);			}		else			{				printf("I do not understand %c please enter a valid number\n\n", select);			}				printf("1 Convert string to all uppercase.\n"		   "2 Convert string to all lowercase.\n"		   "3 Append  and a million dollars. to the string using strncat().\n"		   "4 Print the string in reverse, then print the original sentenceing.\n"		   "5 Take two strings, and determine if the second is in the first.\n"		   "6 To invert the case of your string!\n"		   "7 Quit\n\n");	}	}
     
  6. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/845981

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice