Help on toupper string

This is a discussion on Help on toupper string within the C++ Programming forums, part of the General Programming Boards category; Hi, Here is my code : #include <string> #include <ctype.h> #include <iostream> using namespace std; int main() { int i,j,k; ...

  1. #1
    cooleeze
    Guest

    Question Help on toupper string

    Hi,
    Here is my code :
    #include <string>
    #include <ctype.h>
    #include <iostream>
    using namespace std;

    int main()
    {
    int i,j,k;
    char *string = "hello.my name is Joe. what is your name?";

    // length = strlen(string);
    for (i = 0; i < 1; i++)
    {
    if ((string[i] >= 'a') && (string[i] <= 'z'))
    {
    string[i] = toupper(string[i]);
    }
    }

    for (j = 6; j < 7; j++)
    {
    if ((string[j] >= 'a') && (string[j] <= 'z'))
    {
    string[j] = toupper(string[j]);
    }
    }

    for (k = 22; k < 23; k++)
    {
    if ((string[k] >= 'a') && (string[k] <= 'z'))
    {
    string[k] = toupper(string[k]);
    }
    }
    cout<<"%s\n"<<string;
    //printf("%s\n",string);
    return 0;
    }

    This code works perfectly fine in Turbo C, but when i compile it through Visual C++, it does'nt gives any error, but when i execute the program , the output is not shown , n message appers ... "This program has caused an error , it will close "
    Can anybody help me fix this problem.
    Thanxs.

  2. #2
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    you not allowed to write code like

    Code:
    char* s = "Hello World!\n";
    
    s[4] = '3';
    As s is read only, and modifing s is undefined, so do something like

    Code:
    char s[] = "Hello World!\n";
    char* p = s;
    while(*p != '\0') {
           *p = toupper(*p);
            ++p;
    }

  3. #3
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    You don't need to check if it's upper case when you call toupper as
    it's going to leave it unchanged.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21